Jump to content

helps on chained comboboxes?


tayys

Recommended Posts

hi,

 

I try to create 2 combo box where the second one will depends on the selection of the first one.

 

But it failed to pass the value to cboSub upon the onchange="load();" of cboMain.

 

Anyway I can fix it?

 

Thanks in advance.

 

Here's code for index.php:

<?php
require_once 'connectDB.php';

$id=$_GET['sub_id'];

$sql="SELECT * FROM tb_process";
$result=mysql_query($sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OSSP-E-Store</title>
<script language="JavaScript">
function load()
{
$("#cboMain").change(function() 
{
	$("#cboSub").load("getter.php?choice=" + $("#cboMain").val());
});
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post">
<select name="cboMain" id="cboMain" onchange="load();">
   		<option value="0" selected="selected">----Select Main Process----</option> 
	<?php 
	while ($row = mysql_fetch_array($result)) 
	{
		$pro_id = $row['pro_id'];
		$pro_name = $row['pro_name'];	
		echo "<option value='$pro_id'>$pro_name</option>";
	}
	?>
</select>
<select name="cboSub" id="cboSub" onchange="">
   		<option value="0" selected="selected">----Select Sub Process----</option> 
</select>
</form>
</body>
</html>

 

Here's code for getter.php:

<?php
require_once 'connectDB.php';

$choice = mysql_real_escape_string($_GET['choice']);
echo $choice;
$query = "SELECT * FROM tb_sub_process WHERE pro_id='$choice'";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) 
{
$sub_id = $row['sub_id'];
$sub_name = $row['sub_name'];	
$select = ($id == $sub_id) ? 'selected="selected"' : NULL;
echo "<option value='$sub_id' $select>$sub_name</option>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/249507-helps-on-chained-comboboxes/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.