使用jQuery.post和getJSON进行搜索

时间:2013-09-26 01:48:26

标签: php jquery ajax

我正在尝试构建搜索网站,但我无法使其正常工作。

我希望在提交表单后从POST(或使用GET链接)获取查询。将它发布到php并将json返回给结果div 。 我试图用ajax发布它,但我不能正确。

返回json的url必须包含所有查询值(cars.php?company_id=1&model_id=1&car_category=compact&priceFrom=min&priceTo=max&kmFrom=all&kmTo=all&yearFrom=all&yearTo=all)或返回特定查询(包含表中的所有结果)

$(document).ready(function () {
    $("form1#submit").submit(function () {
        $.ajax({
            type: 'POST',
            url: './system/feeds/cars.results.php',
            data: $(this).serialize(),
            success: function () {
                $.getJSON('./system/feeds/cars.results.php', function (json) {
                    var output = '';
                    for (var i = 0; i < json.car.length; i++) {
                        output += '<div class="grid_12">';
                        output += '<div class="block3">';
                        output += '<a href="' + json.car[i].car_link + '"><img src="images/' + json.car[i].car_image + '" alt="" class="img_fleft img-rounded"></a>';
                        output += '<div class="wrapper border_block">';
                        output += '<div class="marg2">';
                        output += '<div class="grid_6 alpha">';
                        output += '<strong><a href="' + json.car[i].car_link + '">' + json.car[i].car_company + ' ' + json.car[i].car_model + ' ' + json.car[i].car_cc + '</a></strong><br>' + json.car[i].car_category + ', ' + json.car[i].car_combustible + ', ' + json.car[i].car_bhp + ' bhp</br></br>';
                        output += '<hr>';
                        output += '<p>' + json.car[i].car_description + '</p>';
                        output += '<a href="' + json.car[i].car_link + '" class="btn">read more</a>';
                        output += '</div>';
                        output += '<div class="grid_3 omega">';
                        output += '<h4>Χρονολογία:</h4> <a class="btn">' + json.car[i].car_month + '/' + json.car[i].car_year + '</a><br>';
                        output += '<h4>Χιλιόμετρα:</h4> <a class="btn">' + json.car[i].car_km + '</a><br>';
                        output += '<h4>Τιμή:</h4> <a class="btn">€ ' + json.car[i].car_price + '</a><br>';
                        output += '</div>';
                        output += '</div>';
                        output += '</div>';
                        output += '</div>';
                        output += '</div>';
                        output += '<div class="clear"></div>';
                    }
                    $('#cars-results').html(output);
                    $('#car_counter').html(json.car.length);
                });
            }
        });
    });
});

当你发布car.results.php?company_id=1&model_id=1

之类的查询时,json php会返回结果

car.results.php - 演示代码(不是基本的

<? include ('../config.php');

if (isset($_REQUEST['company_id']) && isset($_REQUEST['model_id'])) {
        $company_id = $_REQUEST['company_id'];
        $model_id = $_REQUEST['model_id'];
        $car_category = $_REQUEST['car_category'];
        $priceFrom = $_REQUEST['priceFrom'];
        $priceTo = $_REQUEST['priceTo'];
        $kmFrom = $_REQUEST['kmFrom'];
        $kmTo = $_REQUEST['kmTo'];
        $yearFrom = $_REQUEST['yearFrom'];
        $yearTo = $_REQUEST['yearTo'];

        $result = mysql_query("SELECT * FROM `cars` WHERE `company_id` = '$company_id' AND `model_id` = '$model_id'") or die("Couldn’t get data from database");

    } else {

        $result = mysql_query("SELECT * FROM `cars` ORDER BY `datetime` DESC LIMIT 3") or die("Couldn’t get data from database");
    }

// Create JSON
$json = array();
while($row = mysql_fetch_array($result)) {
    $car = array('car_image'        => $row['car_image'], 
                  'car_link'        => "car.php?id=".$row['car_id'], 
                  'car_company'     => $row['company_id'],
                  'car_model'       => $row['model_id'],
                  'car_cc'          => $row['car_cc'],
                  'car_category'    => $row['car_category'],
                  'car_combustible' => $row['car_combustible'],
                  'car_bhp'         => $row['car_bhp'],
                  'car_description' => $row['car_description'],
                  'car_month'       => $row['car_month'],
                  'car_year'        => $row['car_year'],
                  'car_km'          => $row['car_km'],
                  'car_price'       => $row['car_price']);
    array_push($json,$car);
}
// header('Content-type: application/json');
echo "{\"car\":".json_encode($json)."}";

ob_flush(); ?>

有什么建议吗?

0 个答案:

没有答案