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?

douxi3085
douxi3085 jQuery将是满足您AJAX需求的智能解决方案。
大约 11 年之前 回复

5个回答



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

PHP中的数据库会爆炸并检查每个元素吗?</ p>
</ div>

展开原文

原文

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?

展开原文

原文

Learn jQuery! Seriously, it's really easy.

dongwu9063
dongwu9063 当然,它不是生命,宇宙和一切的答案,但它是一个AJAX库,它抽象了OP遇到的许多问题。
大约 11 年之前 回复
doukougua7873
doukougua7873 jQuery被高估了。 这是一个“简单”的JS框架,而不是生命,宇宙和一切的答案。 如果您无法正确编写Javascript代码,那么针对您的问题抛出框架将不会神奇地修复所有问题。
大约 11 年之前 回复



这里是上面的ajax代码所要求的PHP代码:</ p>

   $ category = $ _GET ['category']; 
$ city = $ _GET ['city'];
$ querystring = $ _GET ['querystring'];
//转义用户输入以帮助防止 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'];

//构建查询
$ 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'$ car s_mileage_to'AND gearbox ='$ cars_gearbox'AND fuel ='$ cars_fuel'“;
//执行查询
$ qry_result = mysql_query($ query)或die(mysql_error());

//构建结果 String
$ display_table =“&lt; table class ='ad_container'&gt;”;

//在表格中为每个结果插入一个新行
while($ row = mysql_fetch_array($ qry_result)){\ nif($ row ['area_id'] =='1'){$ row ['area_id'] =“Göteborg”;}
$ display_table。=“&lt; tr&gt;”;
$ display_table。=“&lt ; td width ='110'rowspan ='2'&gt; BILD&lt; / td&gt;“;
$ display_table。=”&lt; td width ='377'height = '15'&gt; $ row [headline]&lt; / td&gt;“;
$ display_table。=”&lt; td width ='98'winspan ='2'&gt; $ row [area_id]&lt; / td&gt;“;
$ display_table。=”&lt; td width ='67'rowspan ='2'&gt; $ row [insert_date]&lt; / td&gt;“;
$ display_table。=”&lt; / tr&gt;“;
$ display_table。=”&lt; tr&gt;“;

$ display_table。=“&lt; td height ='15'&gt; $ row [price]: - &lt; / td&gt;”;
$ display_table。=“&lt; / tr&gt;”;
}

// echo“查询:”。 $查询。 “&lt; br /&gt;”;
$ display_table。=“&lt; / table&gt;”;
echo $ display_table;
</ code> </ pre>
</ div>

展开原文

原文

And here is the PHP code by the way which the ajax code above calls for:

    $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;

dongzong5017
dongzong5017 它会有助于创建一些断言 - 检查数字输入是否是真正的数值 - is_numeric($ var)
大约 11 年之前 回复



创建这样的通用方法</ p>

  function getElementValue(id)
{

if(document.getElementById(id)!= null)
{
return document.getElementById(id).value;
}
}
</ code> </ pre>

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

展开原文

原文

Create a generic methods like this

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

Then give the unique id for your dropdowns like catagory1 to 10 and then iterate it with loops and use the method.



在javascript中检查每个元素值是否为null,如果(a!='')则只在字符串参数中连接。

使用某个分隔符生成一个参数并在php文件中进行爆炸并根据该进行sql查询。</ p>
</ div>

展开原文

原文

In javascript check the each element value if its not null if(a!='') then only concatenate in string argument . make one argument with some delimiter and explode in php file and according to that make sql query .

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐