从PHP脚本返回JSON和HTML [英] returning JSON and HTML from PHP script
问题描述
我正在尝试使用jQuery发送ajax请求到脚本,该脚本运行mysql查询数据库中的数据,并使用php的json_encode将其序列化为JSON格式。然后使用可用的json2.js脚本解析响应。所有这些工作都很好,但我还想从此脚本中返回除JSON之外的更多数据。
主要是,我还想在json_encode之前回显以下行:
echo< h1 style ='margin-left:25px;'> $ num_rows $ mysql_table< / h1>的注释;
然而,我的jQuery在ajax成功期间正在评估整个响应,导致json.parse函数失败由于脚本的返回格式无效。
成功:函数(数据){
//检索注释通过将它们解析为JSON对象来在页面上显示
var obj = JSON.parse(data);
//遍历JSON数组中的所有项目
for(var x = 0; x< obj.length; x ++){
//为新元素$ b创建一个容器$ b var div = $(< div>)。addClass(bubble)。appendTo(#comments);
//将作者姓名和注释添加到容器
var blockquote = $(< blockquote>)。appendTo(div);
$(< p>).text(obj [x] .comment).appendTo(blockquote);
var cite = $(< cite>)。appendTo(div);
$(< strong>).text(obj [x] .name).appendTo(cite);
$(< i>).text(obj [x] .datetime).appendTo(cite);
}
$(#db)。attr(value,''+ initialComments +'');
}
没有人知道我可以如何返回html行以及json_encode谢谢你,这个网站已经很好的回答了我的noob问题。
my 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 =>日期(m / d / Y g:i A,strtotime($ comment ['datetime'])));
}
// echo< h1 style ='margin-left:25px;'> $ num_rows $ mysql_table< / h1>的注释;
$ response = json_encode($ comments);
echo $ response;`
t echo
该行,将其保存在一个变量中。构造一个简单的数组
$ response = array(
然后发回(通过
'html'=> $ the_line_you_wanted_to_echo,
'jsobject'=> $ the_object_you_were_going_to_send_back
); json_encode
)。
另外, t需要json2.js,jQuery有一个很好的JSON解析器。
可以像这样加载 $。get('your / url',{ params:here},success,'JSON');
更改为与新引入的迭代相匹配。 / p>
for($ x = 0,$ num_rows = mysql_num_rows($ result); $ x <$ num_rows; $ x ++){
$ row = mysql_fetch_assoc($ result);
$ comment [$ x] = array(
name=> stripslashes($ row [name]),
comment=> stripslashes($ row [ (m / d / Y g:我A,strtotime($ comment ['datetime']))
);
datetime=>
}
$ html =< h1 style ='margin-left:25px;'> $ num_rows $ mysql_table< / h1>
echo json_encode(array('comments'=> $ comments,'html'=> $ html));
然后,在您的javascript中,您有
函数成功(parsedObject){
parsedObject.html; //< h1 style ...
parsedObject.comments; //数组对象
parsedObject.comments [0] .name
+on+ parsedObject.comments [0] .datetime
+said \\\
+ parsedObject.comments [0]的.comment; //例如
}
Hi searched through the questions here, but couldn't find anything. I'm new at writing PHP and jQuery, so bear with me.
What I'm trying to do is send an ajax request using jQuery to my script which runs a mysql query on data from my database and serializes it into the JSON format using php's json_encode. The response is then parsed with the available json2.js script. All of this works fine, but I'd also like to return more data other than just JSON from this script.
mainly, i'd like to also echo the following line before the json_encode:
echo "<h1 style='margin-left: 25px;'>$num_rows Comments for $mysql_table</h1>";
however, my jQuery is evaluating the entire response during the ajax success, making the json.parse function fail due to the script's return being in an invalid format.
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 = $("<div>").addClass("bubble").appendTo("#comments");
//Add author name and comment to container
var blockquote = $("<blockquote>").appendTo(div);
$("<p>").text(obj[x].comment).appendTo(blockquote);
var cite = $("<cite>").appendTo(div);
$("<strong>").text(obj[x].name).appendTo(cite);
$("<i>").text(obj[x].datetime).appendTo(cite);
}
$("#db").attr("value", '' + initialComments + '');
}
does anyone know how i can return the html line as well as the json_encode to use this script for more than just json population?
thankyou, this website has been wonderful in answering my noob questions.
my 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 "<h1 style='margin-left: 25px;'>$num_rows Comments for $mysql_table</h1>";
$response = json_encode($comments);
echo $response;`
Don't echo
the line, save it in a variable. Construct a simple array
$response = array(
'html' => $the_line_you_wanted_to_echo,
'jsobject' => $the_object_you_were_going_to_send_back
);
and send that back ( via json_encode
) instead.
Also, you don't need json2.js, jQuery has an excellent JSON parser.
you can load like this $.get( 'your/url', { params : here }, success, 'JSON' );
Changed to match your newly introduced iteration.
for ($x = 0, $num_rows = mysql_num_rows($result); $x < $num_rows; $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']))
);
}
$html = "<h1 style='margin-left: 25px;'>$num_rows Comments for $mysql_table</h1>";
echo json_encode(array( 'comments' => $comments, 'html' => $html ));
then, in your javascript, you have
function success( parsedObject ){
parsedObject.html; // "<h1 style..."
parsedObject.comments; // an array of objects
parsedObject.comments[0].name
+ " on " + parsedObject.comments[0].datetime
+ " said \n" + parsedObject.comments[0].comment; // for example
}
这篇关于从PHP脚本返回JSON和HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!