Php Ajax JSON传递多个变量
大家好,我对ajax和JSON都是新手,我已经按照这本指南制作了一个jquery滑块,并从DB中获得了一些结果。它正在工作,但现在我只能从滑块范围中获得一个结果,但我想从该范围中获得所有结果,所以我需要从php中传递多个结果变量,似乎无法让它工作我的代码包含在下面Php Ajax JSON传递多个变量,php,jquery,html,ajax,json,Php,Jquery,Html,Ajax,Json,大家好,我对ajax和JSON都是新手,我已经按照这本指南制作了一个jquery滑块,并从DB中获得了一些结果。它正在工作,但现在我只能从滑块范围中获得一个结果,但我想从该范围中获得所有结果,所以我需要从php中传递多个结果变量,似乎无法让它工作我的代码包含在下面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transiti
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<script src="slider.js"></script>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div>
<span id="deal_min_price"></span>
<span id="deal_max_price" style="float: right"></span>
<br /><br />
<div id="slider_price"></div>
<br />
<span id="number_results"></span> Abonnementer fundet
</div>
</body>
</html>
$(document).ready(function()
{
$( "#slider_price" ).slider({
range: true,
min: 0,
max: 349,
step:1,
values: [ 0, 349 ],
slide: function( event, ui ) {
$( "#deal_min_price" ).text(ui.values[0] + "KR");
$( "#deal_max_price" ).text(ui.values[1] + "KR");
},
stop: function( event, ui ) {
var dealsTotal = getDeals(ui.values[0], ui.values[1]);
$("#number_results").text(dealsTotal);
},
});
$("#deal_min_price").text( $("#slider_price").slider("values", 0) + "KR");
$("#deal_max_price").text( $("#slider_price").slider("values", 1) + "KR");
});
function getDeals(min_price, max_price)
{
var numberOfDeals = 0;
$.ajax(
{
type: "POST",
url: 'deals.php',
dataType: 'json',
data: {'minprice': min_price, 'maxprice':max_price},
async: false,
success: function(data)
{
numberOfDeals = data;
}
});
return numberOfDeals;
}
PHP:
这就是为什么只得到Selskab-$result=$row['Selskab'] 试试看:
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$result = $row;
if ($result)
{
echo json_encode($result);
}
else
{
echo json_encode(0);
}
您需要循环所有结果或使用PDO::fetchAll,如果需要所有列,还需要使用整行,而不仅仅是Selskab: 或者使用PDO::fetchAll: 但是,您似乎只需要Selskab和id,而不是所有列,在这种情况下,您可能还应该调整查询:
SELECT id, Selskab
FROM mobilabonnement
WHERE Prisprmdr BETWEEN :minprice AND :maxprice
那么你真正的问题是什么呢?@ThomasKilian你现在可以从我的php中看到我得到Selskab,但我还想得到id,等等滑块范围内的所有行
$result = array();
while (false !== ($row = $stmt->fetch(PDO::FETCH_ASSOC))) {
$result[] = $row;
}
if (count($result)
{
echo json_encode($result);
} else{
echo json_encode(0);
}
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($result)
{
echo json_encode($result);
} else{
echo json_encode(0);
}
SELECT id, Selskab
FROM mobilabonnement
WHERE Prisprmdr BETWEEN :minprice AND :maxprice