我收到了从mysqldb到$的10个名称字符串。Ajax (function(data)现在如何分割(data)因为它在一个div中返回所有10个名称我想在10个不同的div中返回它
$(document).ready(function() {
$('ul.sub_menu a').click(function() {
var txt = $(this).text();
//console.log("you clicked"+txt);
$.ajax({
type: 'POST',
url: 'thegamer.php',
data: {send_txt: txt},
success: function( data ){
//now echo the data where you want
// for example
$('#sliderid').html( data );
// or you can see in on firebug
console.log( 'Return:' + data );
}
});
});
});
我的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'
$i=0;
echo "<table>";
if(mysql_num_rows($result) > 0){
//Fetch rows
while($row = mysql_fetch_array($result)){
echo "<tr><td>".$row['imgurl']."</td></tr>";
$i+=1;
}
}else{
echo "<tr><td>No results matching family "$family"</td></tr>";
}
echo "</table>";
有很多方法可以做到这一点:
1)你可以在PHP端做:例如:
foreach($rows as $row)
{
$return_data .= '<div>' . $row['name'] . '</div>';
}
2)你可以返回数据JSON(使用php中的json_encode()),然后循环通过它在js