更改一个选择的值取决于另一个的值,从数据库,而不刷新页面在PHP中

时间:2015-02-05 07:37:03

标签: php mysql

这里' toq'是一个选择标记和' sub_toq'另一个取决于' toq'的价值,想要显示选项,选择' toq'值,在'sub_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>
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在这里,我使用link获得了另一个解决方案......但是,我希望在不重新加载页面的情况下实现这一目标......如何做到这一点?...

&#13;
&#13;
<SCRIPT language=JavaScript>
function reload(form)
   {
	var val=form.cat.options[form.cat.options.selectedIndex].value;
	self.location='index.php?cat=' + val ;
	}										
</script>
&#13;
<?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>";										?>												
&#13;
&#13;
&#13;