我想在任何时候在页面上有三个左右的下拉框。
当我选择第一个下拉框时,它应该在表格视图中显示数据库中的内容。然后,当我选择第二个投递箱时,它应该过滤表格中的数据并在3框中显示表格等。
我知道它可以在Ajax中完成,所以我在php中进行了一些编码。我做了代码,但是有一个问题,第一个盒子正常工作,但是当我选择第二个盒子时,我需要第一个盒子参数和第二个盒子参数,然后发送到第三个选择框。
请从数据库提取动态下拉框中为此提供简单的代码。我正在尝试使用此代码进行用户评论审核。
我使用过此代码 http://www.w3schools.com/PHP/php_ajax_database.asp
但是在我的情况下,一切都来自数据库,选择的值应该过滤2选择框并显示表格。然后当选择3选择框时,它显示取决于前两个选中的框。
我尽可能地尝试解释我想要的东西。我想我清楚我需要的东西。
寻求您的支持。 代码在这里
<html>
<head>
<script type="text/javascript">
function showUserM(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
function showUserI(str1,srt2)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str1+"&i="+str2,true);
xmlhttp.send();
}
function showUserA(str1,str2,str3)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str1+"&i="+str2+"&a="+str3,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$server = 'localhost';
$user = 'root';
$pass = '';
$dbase = 'pub_12wing';
$connect = mysql_connect($server, $user, $pass, $dbase);
mysql_select_db("publisher",$connect);
/ *这里有一些错误,在根据$ sql_issue获取数据时应该依赖于$ sql_magz选择的查询,因此对于最后一个选择框 什么需要改变* /
$sql_magz = "select Distinct magz_id from magz_comment where pub_id = '2'";
$sql_issue = "select Distinct issue_id from magz_comment where pub_id = '2'";
$sql_artical = "select Distinct artical_id from magz_comment where pub_id = '2'";
$result_magz = mysql_query($sql_magz);
$result_issue = mysql_query($sql_issue);
$result_artical = mysql_query($sql_artical);
?>
<form>
<select name="mag_id" onChange="showUserM(document.getElementByID('mag_id').value)" id="mag_id">
<option value="" selected>Select a Magazine ID:</option>
<?php
while ($row =mysql_fetch_array($result_magz)){
echo "<option value=".$row['magz_id'].">{$row['magz_id']}</option>";
}
?>
</select>
<select name="issue_id" onchange="showUserI(document.getElementByID('mag_id').value,document.getElementByID('issue_id').value)" id="issue_id">
<option value="" selected>Select a Issue ID:</option>
<?php
while ($row =mysql_fetch_array($result_issue)){
echo "<option value=".$row['issue_id'].">{$row['issue_id']}</option>";
}
?>
</select>
<select name="artical_id" onchange="showUserA(document.getElementByID('mag_id').value,document.getElementByID('issue_id').value,document.getElementByID('artical_id').value)" id="artical_id">
<option value="" selected>Select a Artical ID:</option>
<?php
while ($row =mysql_fetch_array($result_artical)){
echo "<option value=".$row['artical_id'].">{$row['artical_id']}</option>";
}
?>
</select>
</form>
<br />
<div id="txtHint"><b>Magazine info will be listed here.</b></div>
<?php //mysql_close();?>
</body>
</html>
这个document.getElementByID('mag_id')。值是否需要更改。
提前致谢。
答案 0 :(得分:0)
第一个组合框有固定值吗? 如果是,则在文档中生成它正常:
<select id="mag_id" onchange='PopulatesSecundBox(this)'>
<?php // prints database's data ?>
</select>
然后,如果secund和第三个组合是dinamically,你可以这样做:
<select id="issue_id" onchange='PopulatesThirdBox(this)'>
<!-- Yes, it's empty -->
</select>
<select id="artical_id">
<!-- Yes, it's empty too -->
</select>
然后使用ajax处理事件中的数据库数据并使用DOM使用选项制作魔法......这有点像你需要的吗?