嗨,在这里搜索了所有问题,但什么都找不到。我对编写PHP和jQuery是个新手,请放心。
我要做的是使用jquery向脚本发送一个Ajax请求,该脚本对数据库中的数据运行一个mysql查询,并使用php的json_编码将其序列化为json格式。然后使用可用的json2.js脚本解析响应。所有这些工作都很好,但是我也希望从这个脚本返回更多的数据,而不仅仅是JSON。
主要是,我还想在json_编码之前回显以下行:
echo "
$num_rows Comments for $mysql_table
";但是,我的jquery正在评估Ajax成功期间的整个响应,这使得json.parse函数失败,因为脚本返回的格式无效。
success: function(data) {
//retrieve comments to display on page by parsing them to a JSON object
var obj = JSON.parse(data);
//loop through all items in the JSON array
for (var x = 0; x < obj.length; x++) {
//Create a container for the new element
var div = $("
//Add author name and comment to container
var blockquote = $("
").appendTo(div);$("
").text(obj[x].comment).appendTo(blockquote);
var cite = $("").appendTo(div);
$("").text(obj[x].name).appendTo(cite);
$("").text(obj[x].datetime).appendTo(cite);
}
$("#db").attr("value", '' + initialComments + '');
}
有人知道我如何返回HTML行以及JSON编码,以便使用这个脚本进行JSON填充之外的其他操作吗?
谢谢,这个网站在回答我的noob问题方面非常出色。
我的PHP:
for ($x = 0, $numrows = mysql_num_rows($result); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($result);
$comments[$x] = array("name" => stripslashes($row["name"]), "comment" => stripslashes($row["comment"]), "datetime" => date("m/d/Y g:i A", strtotime($comment['datetime'])));
}
//echo "
$num_rows Comments for $mysql_table
";$response = json_encode($comments);
echo $response;`