0

This jquery function that sends a variable and retrieve data from a php file

function update(){  
    var nid = $("#identity").val(); 
    //alert(nid);
    $.getJSON('../js_backend/getComment.php', {n:nid},function(data){
        $("#comment_view_spot").empty();    
        $.each(data.result, function(){
            $("#comment_view_spot").append("<tr><td>"+this['username']+":</td><td>"+this['comment']+"</td></tr>");  
        });
    }); 
}   

below is the php file it works with, the problem am having is that it works fine when am not getting the variable i sent from the jquery

$result=array();
//$n=$_GET['n'];
$getJs=$connect->query("SELECT * FROM blog_comment");
while($rows=$getJs->fetch()){
    array_push($result, array(
        'username'=>$rows['username'],
        'comment'=>$rows['comment']
    ));
}
header('Content-type: application/json');
echo json_encode(array("result" => $result));

but when am getting the variable it does not work fine if i try to go directly to the php file i see this bunch of codes

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: n in C:\wamp\www\ezoole\js_backend\getComment.php on line <i>6</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>241728</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\ezoole\js_backend\getComment.php' bgcolor='#eeeeec'>..\getComment.php<b>:</b>0</td></tr>
</table></font>

please someone should help me resolve this.

4
  • $.postJSON is not standard jQuery but perhaps if you changed {n:nid} to {"n::nid} it might work, otherwise just use the standard jquery post api.jquery.com/jquery.post Commented Aug 5, 2014 at 22:32
  • 3
    I think the problem is that you "post" your data but you use the superglobal $_GET in your PHP... you should use $_POST instead Commented Aug 5, 2014 at 22:33
  • +1 @Oliboy50 that sounds a much more sensible answer than mine! Commented Aug 5, 2014 at 22:46
  • i have tried your style and corrected the $_GET but still giving the same error Commented Aug 5, 2014 at 22:54

1 Answer 1

1

The problen is that you are using $n=$_GET['n']; but the method you are using is post

Write

$n=$_POST['n'];

Instead of $n=$_GET['n'];

This should work correctly

$.ajax({
url:'../js_backend/getComment.php',
type:'POST',
data:{n:nid},
success: function(data){
$("#comment_view_spot").empty();    
    $.each(data.result, function(){
        $("#comment_view_spot").append("<tr><td>"+this['username']+":</td><td>"+this['comment']+"</td></tr>");
}
});

And on server side use

$n=$_POST['n'];
Sign up to request clarification or add additional context in comments.

1 Comment

i have corrected it but still giving me the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.