dongqiang5541 2009-10-13 09:17
浏览 61
已采纳

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

I am currently creating a type of "classifieds" website, and when the user enters the site he/she is able to choose for example "Vehicles" in a dropdown list, and then choose mileage, year, price range etc...

Here is my problem, I have managed to make contact with a db using Ajax to call a php script which then checks the mysql db for the search criteria and returns a result. BUT, I need to make this as "smart" as possible...

How can i make a function or maybe a loop to check what category the user chose, and then to see if the user made any specifications on price or mileage for example, and then build a query_string to send to the php code, which then checks the mysql database for the search?

Heres a bit of code from the Ajax file:

//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); 
}

Edited to add:

I don't have time to learn jQuery. I am sorry guys, but could you just point me in the right direction when it comes to the coding of this…

What would be the "best" way for doing what I want?

Also, there is an error if all elements in the array that I send to the php file isn't filled with values... If an array is empty, then the script will crash...

Any suggestions on how to solve this easily?

  • 写回答

5条回答

  • doucao8982 2009-10-13 09:23
    关注

    Why not just add the item->value to an array (using jQuery) upon user selection, and pass this array to the php?

    The in the PHP explode the array and check each element?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog