下面是 ajax 代码。
问题在评论中
$.ajax({
type: "POST",
url: "dbtryout2_2.php",
data:datastr,
dataType: "json",
success: function(arrayphp)
{
//ARRAYPHP is a json array and iam displaying the
//contents of the array one by one as link
$.each(arrayphp, function(i, element) {
var link = $('<a href="#" class="album"><font color="red">' + element + '</font></a>');
var image='<input type="image" src="img4.jpg" height="30px" width="30px">';
$(".searchby .searchlist").append(image).append(link);
});
}
});
上面的代码不是 working.am 我没有正确使用json数组下面是PHP代码/PHP代码/
if(!strcmp($_REQUEST['id'],'a2'))
{
$lang=$_REQUEST['lang'];$type=$_REQUEST['type'];
$query="select distinct(cat_name) from song where lang='$lang' and category='$type'";
$result=mysql_query($query,$con) or die("Query failed".mysql_error());
$arrayphp=array();
while($row=mysql_fetch_array($result))
{
$element=$row['cat_name'];
array_push($arrayphp,$element);
}
//here is the json array that is being sent to the above html code
echo json_encode($arrayphp);
}
您需要将从 PHP 传递的字符串转换为正确的 JSON 对象/数组,然后才能用作 JavaScript 对象/数组。
success: function(arrayphp) {
arrayphp = jQuery.parseJSON(arrayphp); // will make proper json from a json string
//ARRAYPHP is a json array and iam displaying the
//contents of the array one by one as link
$.each(arrayphp, function(i, element) {
....