开发者

Strange POST data junk being returned

开发者 https://www.devze.com 2023-04-01 02:32 出处:网络
I\'m writing a fairly basic commenting function for a site using post method to pass input values to a php file that performs the relevant mysql query.

I'm writing a fairly basic commenting function for a site using post method to pass input values to a php file that performs the relevant mysql query.

The $.ajax part is as follows:

// AJAX URL ENCODING
$("#add").click(function() {  
        // POST COMMENT FORM VALUES
        var ajaxOpts = {  
        type: "post",  
        url: "includes/addComment.php",  
        data: "&author=" + $("#author").find("input").val() + "&email=" + $("#email").find("input").val() + "&comment=" + $("#leaveComment").find("textarea").val() + "&parent=<?php echo $vid_id; ?>",  
        success: function(data) {  

            // IMMEDIATE DISPLAY
            // Not relevant to this problem.

            // CLEAR TEXTAREA FOR NEXT COMMENT
            $("textarea").val('');  
        } 


        };  

    $.ajax(ajaxOpts);  
});

This is passed through to addcomment.php:

require('connection.php');    

//get POST data  
$name = mysql_real_escape_string($_POST["author"]); 
$email = strtolower(md5(trim(mysql_real_escape_string($_POST["email"])))); 
$comment = mysql_real_escape_string($_POST["comment"]);
$time = Date ('c');
$parent = trim(mysql_real_escape_string($_POST["parent"]));


//add new comment to database  
mysql_query("INSERT INTO comments VALUES(' $name ',' $email开发者_如何学编程 ',' $comment ', ' $time ', ' $parent ')"); 

Everything works fine except that the mysql_query doesn't end up inserting any values into my database. After a bit of digging I found this:

Strange POST data junk being returned

So i'm assuming my query isn't going ahead because the ' : ' bit of junk(?) data is throwing everything out?

I'm trying to figure out where this errant ' : ' has come from, anyone have any ideas?

P.S - Apologies for the length of this question, I've tried to narrow down possible culprits as much as I can.


You dont have to do that URI encoding yourself. Read the documentation of jquery, the 'data' to be sent could be a map/json.

http://api.jquery.com/jQuery.post/

change

data: "&author=" + $("#author").find("input").val() + "&email=" + $("#email").find("input").val() + "&comment=" + $("#leaveComment").find("textarea").val() + "&parent=<?php echo $vid_id; ?>"

into

{ author: 'something',email:'test' }

or more professionally

$("#testform").serialize() //here #("#testform") stands for a '<form>' tag

And try not to complicate the problem, start with some simple functions, use more echo() and alert() to debug.


what happens if you remove the & from the front of &author?

normally the first data field doesn't have an &

data: "&author=" 

also as others have mentioned this would be a welcome addition

or die (mysql_error()) 

and its usually good practice to name your database columns on the insert otherwise you risk breaking the insert statement if you ever alter the table structure

insert into (colname1, colname2) values ('value1', 'value2')


you can print_r($_POST) to see if the colon is really there. secondly, while debugging it wouldn't hurt to throw an

or die (mysql_error()) 

at the end of your query. Without those two, it's hard to debug.


2 things :

  • ajaxOpts variable is out of scope , you will have a error ( ajaxOpts is not defined)
  • In you example, you miss }); at the end , you should check your JS validate too before checking your php script is working. If you have a JS error, the ajax call will not be called.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号