我已使用选择框将代码更改为以下内容:
<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>
您好,
我已更新代码以反映下面的答案,但现在,当您进行第一次选择时,“模型”选择框仍为空。
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
}
}
?>
答案 0 :(得分:1)
据我所知,问题是您正在加载选择按钮内的整个请求响应文本。我已经查看了你的请求响应,它正在响应整个页面加载模型,所以基本上它是获取所有选项并将它们加载到模型选择框中,因为你将整个页面插入模型选择框
您有多种选择: 您可以创建仅加载模型选项的页面,因此要有一个只包含此部分的文件:
$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>";
}
}
并通过ajax更改您正在呼叫的页面以指向该页面:
xmlhttp.open("GET","newpage.php?ajaxmake="+ makevalue,true);
另一个选项,我建议您查看一些javascript库,例如jQuery,它具有简化您工作的功能。
如果包含jQUery库,则将选择名称添加为id =&#34; makeSelection&#34;和id =&#34; modelSelection&#34;你可以写一个像这样的javascript函数:
jQuery(document).ready(function(){
jQuery("#makeSelection").change(function(){
jQuery("#modelSelection").load("?ajaxmake="+ makevalue + #modelSelection option");
});
});
顺便说一句!请注意,您的SQL查询可能存在严重的安全问题,因为人们可以通过ajaxmake变量攻击您,并截断/删除您的表或任何内容。我建议您清理并验证来自您请求的数据,特别是如果您在互联网上发布一些敏感数据,如数据库表!如果您想了解有关SQL注入的更多信息(如何调用此安全问题):How can I prevent SQL injection in PHP?
答案 1 :(得分:0)
我不确定为什么你的ajax处理文件中包含html。通常你保留一个只包含php代码的.php
文件,然后你就可以确定没有包含html或脚本代码(现在你的页面正在进行)。
首先,尝试将您的model
下拉代码更改为:
<?php
//loads the models based on the makes selection into a dependant dropdown
if (isset($_REQUEST['ajaxmake'])) {
echo "<select name='modelSelection'>"; //select tag placed here
$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>";
}
echo "</select><br>";
die(); //<-- the die placed here will not execute the rest of
//the code and also all the options will be populated
}
?>