Ajax,PHP,Javascript,智能解决方案

时间:2009-10-13 09:17:39

标签: php javascript ajax

我目前正在创建一种“分类广告”网站,当用户进入网站时,他/她可以在下拉列表中选择“车辆”,然后选择里程,年份,价格范围等。 ..

这是我的问题,我已经设法使用Ajax与数据库联系调用php脚本,然后检查mysql数据库的搜索条件并返回结果。 但是,我需要尽可能“聪明”......

如何创建一个函数或循环来检查用户选择的类别,然后查看用户是否对价格或里程做出任何规范,然后构建一个query_string以发送到php代码,然后检查mysql数据库进行搜索?

来自Ajax文件的一些代码:

//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        var ajaxDisplay = document.getElementById('cont');
        ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
}       

var category = document.getElementById('nav_category_list').value;
var city = document.getElementById('nav_city_list').value;
var querystring = document.getElementById('nav_querystring').value;
var cars_price_from = document.getElementById('cars_price_from').value;
var cars_price_to = document.getElementById('cars_price_to').value;
var cars_year_from = document.getElementById('cars_yr_from').value;
var cars_year_to = document.getElementById('cars_yr_to').value;
var cars_mileage_from = document.getElementById('cars_mile_from').value;
var cars_mileage_to = document.getElementById('cars_mile_to').value;
var cars_grbx = document.getElementById('cars_grbx').value;
var cars_fuel = document.getElementById('cars_fuel').value;

var send_query = "?category=" + category + "&city=" + city + "&cars_price_from=" + cars_price_from + "&cars_price_to=" + cars_price_to + "&cars_year_from=" + cars_year_from + "&cars_year_to=" + cars_year_to + "&cars_mileage_from=" + cars_mileage_from + "&cars_mileage_to=" + cars_mileage_to + "&cars_grbx=" + cars_grbx + "&cars_fuel=" + cars_fuel +"&querystring=" + querystring;

ajaxRequest.open("GET", "bincgi/ajax-example.php" + send_query, true);
ajaxRequest.send(null); 
}

编辑添加:

我没有时间学习jQuery。我很抱歉,但是当你谈到编码时,你能指出我正确的方向......

做我想要的“最佳”方式是什么?

此外,如果我发送到php文件的数组中的所有元素都没有填充值,则会出错...如果数组为空,则脚本将崩溃...

有关如何轻松解决此问题的任何建议?

5 个答案:

答案 0 :(得分:3)

为什么不在用户选择时将item->值添加到数组(使用jQuery),并将此数组传递给php?

在PHP中爆炸数组并检查每个元素?

答案 1 :(得分:1)

Learn jQuery!说真的,这很容易。

答案 2 :(得分:0)

以上是ajax代码所要求的PHP代码:

    $category = $_GET['category'];
    $city = $_GET['city'];
    $querystring = $_GET['querystring'];
// Escape User Input to help prevent SQL Injection
$category = mysql_real_escape_string($category);
$city = mysql_real_escape_string($city);
$querystring = mysql_real_escape_string($querystring);
$cars_price_from = $_GET['cars_price_from'];
$cars_price_to = $_GET['cars_price_to'];
$cars_year_from = $_GET['cars_year_from'];
$cars_year_to = $_GET['cars_year_to'];
$cars_mileage_from = $_GET['cars_mileage_from'];
$cars_mileage_to = $_GET['cars_mileage_to'];
$cars_gearbox = $_GET['cars_grbx'];
$cars_fuel = $_GET['cars_fuel'];

//build query
    $query = "SELECT * FROM cars_db WHERE price BETWEEN '$cars_price_from' AND '$cars_price_to' AND year BETWEEN '$cars_year_from' AND '$cars_year_to' AND mileage BETWEEN '$cars_mileage_from' AND '$cars_mileage_to' AND gearbox = '$cars_gearbox' AND fuel = '$cars_fuel'";
//Execute query
    $qry_result = mysql_query($query) or die(mysql_error());

//Build Result String
    $display_table = "<table class='ad_container'>";

// Insert a new row in the table for each result
    while($row = mysql_fetch_array($qry_result)){
if ($row['area_id']=='1') {$row['area_id']="Göteborg";}
$display_table .= "<tr>";
$display_table .= "<td width='110' rowspan='2'>BILD</td>";
$display_table .= "<td width='377' height='15'>$row[headline]</td>";
$display_table .= "<td width='98' rowspan='2'>$row[area_id]</td>";
$display_table .= "<td width='67' rowspan='2'>$row[insert_date]</td>";
$display_table .= "</tr>";
$display_table .= "<tr>";
$display_table .= "<td height='15'>$row[price]:-</td>";
$display_table .= "</tr>";
     }

    //echo "Query: " . $query . "<br />";
    $display_table .= "</table>";
    echo $display_table;

答案 3 :(得分:0)

创建像这样的通用方法

function getElementValue(id)
{
   if(document.getElementById(id) != null) 
   {
     return document.getElementById(id).value;
   }
}

然后为你的下拉列表提供唯一的id,如catagory1到10,然后用循环迭代它并使用该方法。

答案 4 :(得分:0)

在javascript中检查每个元素值,如果它不为null if(a!='')然后只在字符串参数中连接。 用一些分隔符创建一个参数并在php文件中爆炸并根据那个make sql查询。