doupao6698 2014-03-25 07:53
浏览 15
已采纳

用于搜索数据库的多个选择框

I've changed the code with the selection boxes to the below:

<html>

<head>
<script type="text/javascript">

function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.frm.modelSelection.innerHTML=xmlhttp.responseText;
    }
}

var makevalue=document.frm.makeSelection.value;

xmlhttp.open("GET","http://www.autodeal.co.za/newsite/model-selection?ajaxmake="+‌​makevalue,true);
xmlhttp.send();
}

</script>
</head>

<body>

<?php 

$dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb";

// Throws an error if the database cannot be found
if (!file_exists($dbName)) {
    die("Could not find database file.");
}

// Connects to the database
// Assumes there is no username or password
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');

?>

<form action="index.php?option=com_content&view=article&id=99" method="post" name="frm">

<select name="makeSelection" onchange="loadXMLDoc()">

<?php
//Loads the Makes from the database into a dropdown
$resultMake = odbc_exec($conn, "SELECT DISTINCT Make FROM Vehicle ORDER BY Make") or die (odbc_errormsg());
while ($rowMake = odbc_fetch_array($resultMake)) {
    echo "<option value='$rowMake[Make]'>$rowMake[Make]</option>";

}

?>
</select><br />

    <select name="modelSelection">

    </select><br />

    <select name="yearSelection">
        <option>2004</option>
        <option>2005</option>
        <option>2006</option>
        <option>2007</option>
        <option>2008</option>
        <option>2009</option>
        <option>2010</option>
        <option>2011</option>
        <option>2012</option>
        <option>2013</option>
        <option>2014</option>
    </select><br />

    <select name="priceSelection">
        <option>< 5000</option>
        <option>5000 - 20 000</option>
        <option>20 000 - 50 000</option>
        <option>50 000 - 100 000</option>
        <option>100 000 - 200 000</option>
        <option>200 000 - 300 000</option>
        <option>300 000 - 400 000</option>
        <option>400 000 - 500 000</option>
        <option>50 000 - 1 000 000</option>
        <option>> 1 000 000</option>
    </select>

<input type="submit" name="submit" value="Go">
</form>

</body>
</html>

Hi,

I've updated the code to reflect the answers below, but now, when you make the first selection, the Model selection box remains empty.

modelSelection.php

<?php
        $dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb";

        // Throws an error if the database cannot be found
        if (!file_exists($dbName)) {
            die("Could not find database file.");
        }

        $conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');
        //loads the models based on the makes selection into a dependant dropdown
        if (isset($_REQUEST['ajaxmake'])) {

            $resultModel = odbc_exec($conn, "SELECT Model FROM Vehicle WHERE Make = '".$_REQUEST['ajaxmake']."'") or die (odbc_errormsg());

                while ($rowModel = odbc_fetch_array($resultModel)) {

                    echo "<option value='$rowModel[Model]'>$rowModel[Model]</option>";
                    die(); //I'm not sure where to put this because I assume this is the reason why this selection must be first
                }

        }

?>
  • 写回答

2条回答 默认 最新

  • doushadu0901 2014-03-25 09:26
    关注

    As far as I can see, the problem is that you are loading the whole request response text inside a select button. I've looked at your request response and it is responding the whole page with the models loaded, so basically it is getting all options and loading them on the Model select box, because you are inserting the whole page on the model select box.

    You have multiple options here: You can create a page that only loads the Model options, so have a file which has only this part:

                $dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb";
    
                // Throws an error if the database cannot be found
                if (!file_exists($dbName)) {
                    die("Could not find database file.");
                }
    
                $conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');
                //loads the models based on the makes selection into a dependant dropdown
                if (isset($_REQUEST['ajaxmake'])) {
    
                    $resultModel = odbc_exec($conn, "SELECT Model FROM Vehicle WHERE Make = '".$_REQUEST['ajaxmake']."'") or die (odbc_errormsg());
    
                        while ($rowModel = odbc_fetch_array($resultModel)) {
    
                            echo "<option value='$rowModel[Model]'>$rowModel[Model]</option>";
                        }
    
                }
    

    And change the page you are calling through ajax to point to that page:

     xmlhttp.open("GET","newpage.php?ajaxmake="+ makevalue,true);
    

    The other option, and the one I suggest you is to look into some javascript library, such as jQuery which has functions to easen your work.

    If you include jQUery library, adding the select name as id="makeSelection" and id="modelSelection" you could write a javascript function like this:

    jQuery(document).ready(function(){
         jQuery("#makeSelection").change(function(){
             jQuery("#modelSelection").load("?ajaxmake="+ makevalue + #modelSelection option");
         });
     });
    

    BTW! Be aware that you may have a huge security problem in your sql queries, since people can attack you through the ajaxmake variable, and truncate/drop your tables or anything. I suggest you to sanitize and validate the data coming from your requests, specially if you post some sensitive data like your database tables on the internet!!! If you want to know more about SQL Injection (how this security issue is called): How can I prevent SQL injection in PHP?

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大