更改一个select的值取决于数据库中另一个的值,而不刷新php中的页面


change the values of one select depends on value of another, from database, without refreshing page in php

这里‘toq’是一个选择标记,‘sub_toq’是另一个。取决于‘toq‘的值,想要在‘sub_to’中显示选择了‘toq"值的选项,但值必须来自数据库。。。。如何做到这一点。。在php中?

<scipt>
$("#select1").change(function() { 
if(typeof $(this).data('options') === "undefined"); 
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
																			$('#select2').html(options);});
</scipt>
<select id="toq">
<?php  
$conn=mysql_connect("localhost","root","")or die("unable to connect to my sql");
if(!$conn)	{	die("Cannot connect to db");}															mysql_select_db("iknow",$conn) or die("UNABLE TO SELECT DB");														$sqlquery = "SELECT header FROM h1 ORDER BY header asc";														
$res = mysql_query($sqlquery);
while($dsatz = mysql_fetch_array($res))
{																	
?>
<OPTION VALUE="<?php echo $dsatz["header"];?>"><?php echo $dsatz["header"];?></OPTION><br>
<?php
}
mysql_close();?>
</select>
<select id="sub_toq">
<?php  
$conn=mysql_connect("localhost","root","")or die("unable to connect to my sql");
if(!$conn)	{	die("Cannot connect to db");}															mysql_select_db("iknow",$conn) or die("UNABLE TO SELECT DB");														$sqlquery = "SELECT * FROM sub ORDER BY sub asc";														
$res = mysql_query($sqlquery);
while($dsatz = mysql_fetch_array($res))
{																	
?>
<OPTION VALUE="<?php echo $dsatz["header"];?>"><?php echo $dsatz["subheader"];?></OPTION><br>
<?php
}
mysql_close();?>
</select>

您可以确保在加载页面时sub_toq的信息已经存在,并使用javascript/jQuery显示正确的信息,也可以使用AJAX从数据库中获取结果,而无需刷新页面。

在这里,我得到了另一个使用链接的解决方案。。。但是,我想在不重新加载页面的情况下实现这一点。。。如何做到这一点?。。。

<SCRIPT language=JavaScript>
function reload(form)
   {
	var val=form.cat.options[form.cat.options.selectedIndex].value;
	self.location='index.php?cat=' + val ;
	}										
</script>
<?php require 'config.php';  ?>
<?Php
@$cat=$_GET['cat']; //echo "cat :" . @$cat=$_GET['cat']; // Use this line or below line if register_global is off
if(strlen($cat) > 0 and !is_numeric($cat)){ 
// to check if $cat is numeric data or not. 
echo "Data Error";
exit;}
$quer2="SELECT DISTINCT Header,PO FROM h1 order by Header"; 			
												
echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example  action=dd-check.php////
///Starting of first drop downlist ////
echo "<select name='cat' onchange='"reload(this.form)'"><option value=''>Select one</option>";
foreach ($dbo->query($quer2) as $noticia2) {
													if($noticia2['PO']==@$cat){
echo "<option selected value='$noticia2[PO]'>$noticia2[Header]</option>"."<BR>";
}
else{
echo  "<option value='$noticia2[PO]'>$noticia2[Header]</option>";
}
}
echo "</select>";
/////  This will end the first drop down list///////					
//// Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
foreach ($dbo->query($quer) as $noticia) {
echo  "<option value='$noticia[Subheader]'>$noticia[Subheader]</option>";
	}
echo "</select>";
////  This will end the second drop down list /////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";										?>