我想创建一个基于 mysql 数据库和表的动态下拉列表。我在网上搜索,最接近的是http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php
我已经按照示例使用了此代码,第一个下拉框可以正常工作,但是一旦选择了“类别”,第二个下拉框就不会被填充。
代码是:
主文件
<html>
<body>
<script type="text/javascript">
function AjaxFunction(cat_id) {
var httpxml;
try {
// Firefox, Opera 8.0+, Safari
httpxml = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
httpxml = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
httpxml = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateck() {
if (httpxml.readyState == 4) {
var myarray = eval(httpxml.responseText);
// Before adding new we must remove previously loaded elements
for (j = document.testform.subcat.options.length - 1; j >= 0; j--) {
document.testform.subcat.remove(j);
}
for (i = 0; i < myarray.length; i++) {
var optn = document.createElement("OPTION");
optn.text = myarray[i];
optn.value = myarray[i];
document.testform.subcat.options.add(optn);
}
}
}
var url="dd.php";
url = url+"?cat_id="+cat_id;
url = url+"&sid="+Math.random();
httpxml.onreadystatechange = stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
}
</script>
<form name="testform" method='POST' action='mainck.php'>Name:<input type=text name=fname>
Select first one <select name=cat onchange="AjaxFunction(this.value);">
<option value=''>Select One</option>
<?
require "config.php";// connection to database
$q=mysql_query("select * from categories");
while($n=mysql_fetch_array($q)){
echo "<option value=$n[cat_id]>$n[category]</option>";
}
?>
</select>
<select name=subcat>
</select><input type=submit value=submit>
</form>
</body>
</html>
和 dd.php 是
<?
$cat_id=$_GET['cat_id'];
require "config.php";
$q=mysql_query("select subcategory from subcategory where cat_id='$cat_id'");
echo mysql_error();
$myarray=array();
$str="";
while($nt=mysql_fetch_array($q)){
$str=$str . "\"$nt[subcategory]\"".",";
}
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string
echo "new Array($str)";
?>
如前所述,main.php 正确加载并填充第一个下拉框。一旦选择了一个值,第二个框中就不会出现任何内容。为了测试,我将 dd.php 中的 mysql 查询从
$q=mysql_query("select subcategory from subcategory where cat_id='$cat_id'");
至
$q=mysql_query("select subcategory from subcategory where cat_id=1");
然后在选择“类别”时填充第二个框。我认为选择的值没有正确地从 main.php 传递到 dd.php
$cat_id=$_GET['cat_id'];
对此的任何帮助将不胜感激。我有一种感觉,这是一件很小的事情,但我无法完全理解它。
一如既往地提前感谢。 更新的问题
主文件
<form name='testform' method='POST' action='mainck.php'>
Name: <input type='text' name='fname'>
Select first one
<select name='cat' onchange='AjaxFunction(this);'>
<option value=''>Select One</option>
<?php
require "config.php";// connection to database
// I will continue to use mysql_query(), but please migrate you code to
// PDO or MySQLi ASAP
$query = "
SELECT cat_id,category
FROM categories
";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo "<option value='{$row['cat_id']}'>{$row['category']}</option>";
}
?>
</select>
<select name='subcat' id='subcat_select'>
</select>
<input type='submit' value='Submit'>
</form>
dd.php
<?php
require "config.php";
$query = "
SELECT packcode
FROM skudata
WHERE cat_id='".mysql_real_escape_string($_GET['cat_id'])."' ";
$result = mysql_query($query);
$array = array();
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row['packcode'];
}
echo json_encode($array);
?>
随着 Dave 添加的更改,我无法让新的 mysql 表和引用列正常工作。已经测试了mysql,它运行良好。任何帮助表示赞赏。
谢谢,