i know that this question is a bit childish but i am unable to find the correct solution to this problem...
我知道这个问题有点幼稚,但我找不到解决这个问题的正确方法……
i am using jquery and ajax call to user search functionality in website with php returning json objects...
我在网站上使用jquery和ajax调用用户搜索功能,php返回json对象……
when i search users using php file, if the json return is only one array, the jquery prints it on the screen, but when multiple results are returned, i don't know to print them out....
当我搜索用户使用php文件,如果json返回只有一个数组,jquery打印在屏幕上,但是当返回多个结果,我不知道打印出来....
here are the results returned from the php:
以下是php返回的结果:
{"search":"10
junaid
saleem
junaid@yahoo.com
"}{"search":"13
zzz
aaa
zzz@yahoo.com
"}
and here is the jquery webpage:
这是jquery网页:
<?php
session_start();
require("secure_scripts/getusers.php");
require("secure_scripts/getdp.php");
require("secure_scripts/getusersinfo.php");
if(!isset($_SESSION['id'])){
header("location: index.php");
}else{
$zxcv_lgn = base64_encode($_SESSION['id']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome <?php echo getusers("first_name"); ?> | Addressbook.com</title>
<script src="jquery.js" type="text/javascript" ></script>
<link rel="stylesheet" href="style.css">
<script type="text/javascript">
$(document).ready(function(){
$("#search_button").click(function(){
$("#search_button").click(function(){ $("#console_loader").hide(); });
$("#console_loader").fadeIn("slow").html("<img src='images/ajax-loader.gif' id='ajax-loader' />");
var send = $("#search").val();
$.ajax({
type: "POST",
url: "secure_scripts/search_users.php",
data: {search: send},
dataType: "json",
success: function(msg){
$("#ajax-loader").fadeOut("slow", function(){
$("#console_loader img").remove();
$("#console_loader").fadeIn("slow").html(msg.search);
});
}
});
});
});
</script>
</head>
<body>
<div id="header">
<p><a href="index.php"><img src="images/header_logo.png" /><span>AddressBook™</span></a></p>
</div>
<div id="wrapper" align="center">
<div id="main">
<div id="content">
<div id="top_nav">
<div class="userinfo"><span class="user_title">Welcome <?php echo getusers("first_name")." ".getusers("last_name"); ?></span></div>
<div class="search">
<form onsubmit="return false" id="search_form">
<input type="text" name="search" class="search_box" id="search" placeholder="Type in to search...">
<input type="button" id="search_button" class="sea" name="search_submit"value="search">
</form>
</div>
</div>
<div id="left_nav">
<div id="dp"><img src="<?php echo getdp(); ?>"></div>
<div class="left_nav_links">Profile</div>
<div class="left_nav_links">Contacts</div>
<div class="left_nav_links">Settings</div>
<div class="left_nav_links">privacy</div>
<div class="left_nav_links">logout</div>
</div>
<div id="console">
<div id="console_top_nav">Your Contacts:</div>
<div id="console_content">
<div id="console_loader" style="display: none;"></div>
</div>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="links"><ul><li><a href="index.php">Home</a></li><li><a href="about">About</a></li><li><a href="contact">Contact</a></li></ul></div>
<div id="copyrights">© 2014 Addressbook.com All Rights Reserved</div>
</div>
</div>
</body>
</html>
whenevery only one object is returned from php, like:
当每个对象从php返回时,如:
{"search":"13
zzz
aaa
zzz@yahoo.com
"}
it works perfectly, but not with multiple json objects....
完美的工作,但不是用多个json对象....
thanks in advance!
提前谢谢!
2 个解决方案
#1
0
Something like this should work:
像这样的东西应该可以:
$.ajax({
type: "POST",
url: "secure_scripts/search_users.php",
data: {search: send},
dataType: "json",
success: function(msg){
$.each(function() {
$("#ajax-loader").fadeOut("slow", function(){
$("#console_loader img").remove();
$("#console_loader").fadeIn("slow").html(msg.search);
});
});
}
});
We're adding $.each()
method to the success function in order to run it for each JSON object that gets returned, not just the first.
我们在success函数中添加了$.each()方法,以便为返回的每个JSON对象运行它,而不仅仅是第一个。
Here's the jQuery.each() doc for further reading.
下面是jQuery.each() doc以供进一步阅读。
edited for clarity
编辑为了清晰
#2
1
You need to use jQuery's .each()
method, like this:
您需要使用jQuery的.each()方法,如下所示:
$(document).ready(function(){
$("#search_button").click(function(){
$("#search_button").click(function(){ $("#console_loader").hide(); });
$("#console_loader").fadeIn("slow").html("<img src='images/ajax-loader.gif' id='ajax-loader' />");
var send = $("#search").val();
$.ajax({
type: "POST",
url: "secure_scripts/search_users.php",
data: {search: send},
dataType: "json",
success: function (msg) {
$.each(function (index, item) {
$("#ajax-loader").fadeOut("slow", function () {
$("#console_loader img").remove();
$("#console_loader").fadeIn("slow").html(item.search);
});
});
}
});
});
});
When your json is received, it is more than likely an array of objects
当收到json时,它很可能是一个对象数组
[{"search":"10
junaid
saleem
junaid@yahoo.com
"}{"search":"13
zzz
aaa
zzz@yahoo.com
"}]
Therefore, by using $.each()
to loop through the collection and return the value (index, item)
you can get the object's value by referencing it like so:
因此,通过使用$.each()循环遍历集合并返回值(index, item),您可以像这样引用对象的值,从而获得对象的值:
$("#console_loader").fadeIn("slow").html(item.search);
since json is returning a JavaScript object literal.
由于json返回的是JavaScript对象文本。
#1
0
Something like this should work:
像这样的东西应该可以:
$.ajax({
type: "POST",
url: "secure_scripts/search_users.php",
data: {search: send},
dataType: "json",
success: function(msg){
$.each(function() {
$("#ajax-loader").fadeOut("slow", function(){
$("#console_loader img").remove();
$("#console_loader").fadeIn("slow").html(msg.search);
});
});
}
});
We're adding $.each()
method to the success function in order to run it for each JSON object that gets returned, not just the first.
我们在success函数中添加了$.each()方法,以便为返回的每个JSON对象运行它,而不仅仅是第一个。
Here's the jQuery.each() doc for further reading.
下面是jQuery.each() doc以供进一步阅读。
edited for clarity
编辑为了清晰
#2
1
You need to use jQuery's .each()
method, like this:
您需要使用jQuery的.each()方法,如下所示:
$(document).ready(function(){
$("#search_button").click(function(){
$("#search_button").click(function(){ $("#console_loader").hide(); });
$("#console_loader").fadeIn("slow").html("<img src='images/ajax-loader.gif' id='ajax-loader' />");
var send = $("#search").val();
$.ajax({
type: "POST",
url: "secure_scripts/search_users.php",
data: {search: send},
dataType: "json",
success: function (msg) {
$.each(function (index, item) {
$("#ajax-loader").fadeOut("slow", function () {
$("#console_loader img").remove();
$("#console_loader").fadeIn("slow").html(item.search);
});
});
}
});
});
});
When your json is received, it is more than likely an array of objects
当收到json时,它很可能是一个对象数组
[{"search":"10
junaid
saleem
junaid@yahoo.com
"}{"search":"13
zzz
aaa
zzz@yahoo.com
"}]
Therefore, by using $.each()
to loop through the collection and return the value (index, item)
you can get the object's value by referencing it like so:
因此,通过使用$.each()循环遍历集合并返回值(index, item),您可以像这样引用对象的值,从而获得对象的值:
$("#console_loader").fadeIn("slow").html(item.search);
since json is returning a JavaScript object literal.
由于json返回的是JavaScript对象文本。