此jquery函数发送变量并从php文件中检索数据
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>");
});
});
}
下面是它使用的php文件,我遇到的问题是它没有得到我从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));
但是当我得到变量时,如果我尝试直接转到php文件,我看到这堆代码
<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>
请有人帮我解决这个问题。
答案 0 :(得分:1)
问题在于您使用的是$n=$_GET['n'];
,但您使用的方法是
写
$n=$_POST['n'];
而不是$n=$_GET['n'];
这应该可以正常工作
$.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>");
}
});
在服务器端使用
$n=$_POST['n'];