以下是我的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);
}
});
怎么办?
答案 0 :(得分:0)
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遍历接收到的数组。
$.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);
});
}
});