我正在尝试建立一个网站搜索,但无法使其正常工作。
提交表单后,我想从POST
(或使用链接)获取查询。GET
将其发布到 php 并将 json 返回到 results 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);
});
}
});
});
});
当您发布类似的查询时,json php会返回结果car.results.php?company_id=1&model_id=1
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(); ?>
有什么建议么 ?