在html页面上将php返回的文本显示为单独的链接
下面是我的AJAX代码。我已经把问题写在评论里了在html页面上将php返回的文本显示为单独的链接,php,jquery,html,hyperlink,Php,Jquery,Html,Hyperlink,下面是我的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
$.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);
}
});
$.ajax({
类型:“POST”,
url:“dbtryout2_2.php”,
数据:datastr,
成功:功能(ArrayHP){
//在这里,从循环中一个接一个地接收来自php的文本
//每个文本项在此处显示为链接
//文本显示为链接是正常的
//但问题是,从php返回的所有文本都显示为
//一个链接,即一个“');
linkclass=link.attr(“类”);
$(“.searchby.searchlist”).append(链接);
}
});
怎么办?Php会将所有内容立即抛出到ajax成功函数中……您需要在Php数组上使用json_编码,并将其作为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调用中,将数据类型设置为“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);
});
}
});
$.ajax({
类型:“POST”,
url:“dbtryout2_2.php”,
数据:datastr,
数据类型:“json”,
成功:函数(arrayjson){
$.each(函数(arrayjson,函数(i,处理的值){
变量链接=$('');
linkclass=link.attr(“类”);
$(“.searchby.searchlist”).append(链接);
});
}
});
发布相关php代码或澄清代码中的注释-不确定您想要实现什么。