views:

52

answers:

2

the code below is a jquery POST request javascript. i want to use the data I am posting in the callback function. if u take a look,

$('#fb_user_msg').innerHTML = data.comment;

the above line is trying to include the comment in the html (unsuccessfully). i am sure this is easy but I dont know why I am not getting it right.

$("#submit_js").click(function() {
    $.post(
        "user_submit.php", 
        {comment: $("#comment").val(), aid: imgnum}, 
        function(data){
             /*alert(data);*/
             //$('#greetings').html('Your choice was submitted successfully. Thank You for voting.');
             $('#confirm_msg').addClass("on");
             $('#care_parent').addClass("off");
             $('#fb_user_msg').innerHTML = data.comment;
        }
    );
});

please help??

A: 

Does the service you are calling (user_submit.php) return the comment data in its response?

Even so, if you already have the data in another area (#comment), why not just take it straight from there?

$('#fb_user_msg').innerHTML = $("#comment").val();
mopoke
#comment is a text area. user_submit.php receives the comment data through POST and adds it to the DB. i didnt understand your point.
amit
But does it return the comment data back in its response? The code you have at the moment is expecting the data object to include that comment. If your php page is not echoing the comment back then I don't see how that would work.
mopoke
i dont want the php to echo it back. i just want to re-use the data i m sending to php in the javascript itself.
amit
OK. So your current code won't work. Have you tried taking it from the comment area directly as I showed in my answer?
mopoke
i tried it as the very first thing. didnt work.
amit
Have you tried debugging in firebug or similar? What is the value of $('#comment').val()? Is the data getting posted to the PHP page succesfully? Do you have any clue as to where it's failing?
mopoke
A: 

You would want to make a global variable before you POST your data in order to use in your callback function:

$("#submit_js").click(function() {
    var comment = $('#comment').val();
    $.post("user_submit.php", {comment: comment, aid: imgnum}, 
    function(data){
        $('#confirm_msg').addClass("on");
        $('#care_parent').addClass("off");
        $('#fb_user_msg').innerHTML = comment;
    });
});
cballou
this doesnt help either.
amit
this worked now. thanks a lot. i made a slight syntax mistake.
amit