简单php返回json,从PHP脚本返回JSON和HTML

嗨,在这里搜索了所有问题,但什么都找不到。我对编写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 = $("

").addClass("bubble").appendTo("#comments");

//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;`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值