简体   繁体   English

JSON解码php问题

[英]JSON decode php problems

I have index.php and getting problem with to decode the json array.. please help i am new to this.. 我有index.php并在解码json数组时遇到问题..请帮助我是新手。

<script>
$(document).ready(function () {
    $("#slider_price").slider({
        range: true,
        min: 0,
        max: 100,
        step: 1,
        values: [0, 100],
        slide: function (event, ui) {
            $("#app_min_price").text(ui.values[0] + "$");
            $("#app_max_price").text(ui.values[1] + "$");
        },
        stop: function (event, ui) {
            var nr_total = getresults(ui.values[0], ui.values[1]);

            $("#results").text(nr_total);
        },
    });
    $("#app_min_price").text($("#slider_price").slider("values", 0) + "$");
    $("#app_max_price").text($("#slider_price").slider("values", 1) + "$");
});

function getresults(min_price, max_price) {
    var number_of_estates = 0;
    $.ajax({
        type: "POST",
        url: 'search_ajax.php',
        dataType: 'json',
        data: {
            'minprice': min_price,
            'maxprice': max_price
        },
        async: false,
        success: function (data) {
            number_of_estates = data;
        }
    });
    return number_of_estates;
}

And search_ajax.php 和search_ajax.php

<?php
 require_once('includes/commonFunctions.php');
// take the estates from the table named "Estates"
if(isset($_POST['minprice']) && isset($_POST['maxprice']))
{
 $minprice  = filter_var($_POST['minprice'] , FILTER_VALIDATE_INT);  
 $maxprice  = filter_var($_POST['maxprice'] , FILTER_VALIDATE_INT); 
 $query = mysql_query("SELECT * FROM cars WHERE min_range >= $minprice AND max_range     <= $maxprice");

$rows = array();
while($r = mysql_fetch_assoc($query)) {
  $rows[] = $r;
}

echo json_encode($rows);

}
?>

and the problem is i just want to print $rows in specific div "number_results".. how to decode that json array? 问题是我只想在特定的div“ number_results”中打印$ rows ..如何解码该json数组?

are you sure about the data you are passing is in json format 您确定要传递的数据为json格式吗

i think it should be 我认为应该

'{"minprice": "min_price", "maxprice":"max_price"}'

you cannot just return ajax returned value from a function since ajax is async...the function will already return number_of_estates , by the time ajax call completes. 您不能仅仅从函数返回ajax返回的值,因为ajax是异步的...在ajax调用完成时,该函数已经返回number_of_estates

use callback or just call a function and append your returned text there 使用回调或仅调用一个函数并在其中附加您返回的文本

..
stop: function( event, ui ) {
  getresults(ui.values[0], ui.values[1]);
},
...

function getresults(min_price, max_price)
{ 
   var number_of_estates = 0;
   $.ajax({
     type: "POST",
     url: 'search_ajax.php',
     dataType: 'json',
     data: {'minprice': min_price, 'maxprice':max_price},
     async: false,
     success: function(data)
     {
        number_of_estates = data;
        $("#results").text(number_of_estates);
     }
  });
 }

however ajax is called each time the stop funnction occurs so be careful. 但是,每次发生停止功能时都会调用ajax,所以要小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM