我的这个PHP代码适用于$ .ajax调用,该代码低于此代码
$family = mysql_real_escape_string($_REQUEST['send_txt'], $link);
$query = "SELECT imgurl FROM images WHERE family='$family'";
//Query database
$result = mysql_query($query, $link);
//Output result, send back to ajax as var 'response'
$imgurl=array();
//$i=0;
if(mysql_num_rows($result) > 0){
//Fetch rows
while($row = mysql_fetch_array($result)){
$imgurl[]=$row['imgurl'];
}
}
echo $imgurl;
jquery代码
$(document).ready(function() {
$('ul.sub_menu a').click(function() {
var txt = $(this).text();
$.ajax({
type: "POST",
url: "thegamer.php",
data:{send_txt: txt},
success: function(data){
$('#main-content').html(data);
}
});
});
});
它只输出在#main-content div中写的数组如何使用该数组基本上是图像路径
答案 0 :(得分:0)
直接从浏览器尝试页面。使用JSON可以在这里提供帮助:
echo json_encode($imgurl);
并使用getJSON
代替普通ajax
:
$.getJSON('thegamer.php', {send_text:text}, function(data) { … });
答案 1 :(得分:0)
为什么要从mysql结果创建数组?你的代码可以更简单:
<?php
$family = mysql_real_escape_string($_REQUEST['send_txt'], $link);
$query = "SELECT imgurl FROM images WHERE family='$family'";
//Query database
$result = mysql_query($query, $link);
//Output result, send back to ajax as var 'response'
if(mysql_num_rows($result) > 0)
{
//Fetch rows
while($row = mysql_fetch_array($result))
{
echo $row['imgurl'];
}
}
?>