Ajax JSON传递多个变量

时间:2015-02-07 17:35:01

标签: 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-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:

<?php


 $result = 0;


 define('MYSQL_HOST',     'db564596075.db.1and1.com');
 define('MYSQL_USER',     'dbo564596075');
 define('MYSQL_PASSWORD', '12345678');
 define('MYSQL_DB',       'db564596075');


try 
    {
    $dbh = new PDO('mysql:host='.MYSQL_HOST.';dbname='.MYSQL_DB, MYSQL_USER, MYSQL_PASSWORD);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->setAttribute(PDO::ATTR_PERSISTENT, true);
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    $dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
} 

catch (PDOException $e) 
{
echo 'Fejk: ' . $e->getMessage() . '<br/>';
}


if(isset($_POST['minprice']) && isset($_POST['maxprice']))
{

$minprice   = filter_var($_POST['minprice'] , FILTER_VALIDATE_INT);  
$maxprice   = filter_var($_POST['maxprice'] , FILTER_VALIDATE_INT); 
$query = '
        SELECT 
        *
        FROM
            mobilabonnement
        WHERE
            Prisprmdr
        BETWEEN 
            :minprice 
        AND
            :maxprice
                    ';

$stmt = $dbh->prepare($query);
try
{
    $stmt->bindParam(':minprice', $minprice);
    $stmt->bindParam(':maxprice', $maxprice);
    $stmt->execute();
}
catch (PDOException $e)
{
    print($e->getMessage());
    die;
}

$row = $stmt->fetch(PDO::FETCH_ASSOC);
$result = $row['Selskab'];
}
if ($result == true)
{
echo json_encode($result);
 }

 else{
 echo json_encode(0);
 }


?>

2 个答案:

答案 0 :(得分:-1)

这就是为什么你只有Selskab - $result = $row['Selskab'];

试试:

$row = $stmt->fetch(PDO::FETCH_ASSOC);
$result = $row;

if ($result)
{
   echo json_encode($result);
}

else
{
  echo json_encode(0);
}

答案 1 :(得分:-1)

您需要循环显示所有结果或使用PDO::fetchAll,如果您希望所有列都需要使用整行而不只是Selskab

$result = array();
while (false !== ($row = $stmt->fetch(PDO::FETCH_ASSOC))) {
   $result[] = $row;
}

if (count($result)
{
   echo json_encode($result);
} else{
  echo json_encode(0);
}

或使用PDO::fetchAll

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (count($result)
{
   echo json_encode($result);
} else{
  echo json_encode(0);
}

但是,您似乎真的只想要Selskabid而不是所有列,在这种情况下,您也应该调整查询:

SELECT id, Selskab
FROM  mobilabonnement
WHERE Prisprmdr BETWEEN :minprice AND :maxprice