I'm trying to create a drop down list of departments and based on this value the sub-department should show. I'm trying to get all values from database. I'm unable to pass values from one table to another so that the sub-department will show values as per the selected department field. I think may be I'm passing wrong value in the java script and at the onchange attribute. what should be the correct way to pass the value from one table to another? sorry for any formatting error.
function department()
{
global $con;
$sql_dept=mysqli_query($con,"SELECT * FROM jsrao_dept");
echo"<select id='test' name='dept_id' onchange='subdept().value'>";
while($row_dept = mysqli_fetch_array($sql_dept))
{
?><option value="<?php $row_dept['Id'] ?>"><?php
echo $row_dept["dept_name"]; ?>
</option><?php
}
echo"</select>";
}
function subdepartment($id)
{
global $con;
global $id_num;
$sql_sdept=mysqli_query($con,"SELECT js.sub_name FROM jsrao_sdept AS js INNER JOIN jsrao_dept AS jd ON jd.Id=js.dept_id where js.dept_id=$id");
echo"<select name='sdept_id'>";
while($row_sdept = mysqli_fetch_array($sql_sdept))
{
?><option value="<?php $row_sdept['Id'] ?>"><?php
echo $row_sdept["sub_name"];
echo"</option>";
}
echo"</select>";
}
<script type="text/javasript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
function subdept(id)
{
<?php
subdepartment(id);
?>
}
</script>
HTML Code:
<tr>
<td>Department<sup style="color:red">*</sup>-</td>
<td><?php
department();
?></td>
</tr>
<tr>
<td>Sub-department-</td>
<td><?php
$sql_dept=mysqli_query($con,"SELECT * FROM jsrao_dept");
$sql_1=mysqli_fetch_array($sql_dept);
$id=$sql_1['Id'];echo $id;
subdepartment($id)
?></td>