下面是我的AJAX代码。我已经把问题写在评论里了。
$.ajax({
type: "POST",
url: "dbtryout2_2.php",
data: datastr,
success: function(arrayphp) {
//here one by one text is being received from php from within a loop
//each text item is getting displayed as link here
//it is OK that text is getting displayed as link
//BUT the problem is that all the text returned from php are getting displayed as
//one link i.e between one "<a></a>"
//I WANT THAT EACH TEXT SHOULD BE DISPLAYED AS SEPERATE LINKS
//AS EACH TEXT IS GETTING RETURNED FROM WITHIN SEPARATE ITERATIONS OF PHP LOOP
var link = $('<a href="#" class="album"><font color="red">' + arrayphp + '</font></a>');
linkclass = link.attr("class");
$(".searchby .searchlist").append(link);
}
});
该怎么办?
Php将所有内容同时抛出到ajax成功函数。。。您需要在php数组上使用json_encode,并将其作为json数组进行回显。
<?php
$return = array();
foreach($array as $key=>$value){
//Do your processing of $value here
$processed_value = $value;
array_push($return, $processed_value);
}
echo json_encode($return);
?>
在ajax调用中,将dataType设置为"json",以便将php传递的数据解析为json数组。使用jquery each在接收的数组中循环。
$.ajax({
type: "POST",
url: "dbtryout2_2.php",
data: datastr,
dataType: "json",
success: function(arrayjson) {
$.each(function(arrayjson, function(i, processed_value) {
var link = $('<a href="#" class="album"><font color="red">' + processed_value + '</font></a>');
linkclass = link.attr("class");
$(".searchby .searchlist").append(link);
});
}
});