Note that there are some explanatory texts on larger screens.

plurals
  1. POdependent drop down list values from database
    primarykey
    data
    text
    <p>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 <strong>onchange</strong> attribute. what should be the correct way to pass the value from one table to another? sorry for any formatting error.</p> <pre><code> function department() { global $con; $sql_dept=mysqli_query($con,"SELECT * FROM jsrao_dept"); echo"&lt;select id='test' name='dept_id' onchange='subdept().value'&gt;"; while($row_dept = mysqli_fetch_array($sql_dept)) { ?&gt;&lt;option value="&lt;?php $row_dept['Id'] ?&gt;"&gt;&lt;?php echo $row_dept["dept_name"]; ?&gt; &lt;/option&gt;&lt;?php } echo"&lt;/select&gt;"; } 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"&lt;select name='sdept_id'&gt;"; while($row_sdept = mysqli_fetch_array($sql_sdept)) { ?&gt;&lt;option value="&lt;?php $row_sdept['Id'] ?&gt;"&gt;&lt;?php echo $row_sdept["sub_name"]; echo"&lt;/option&gt;"; } echo"&lt;/select&gt;"; } &lt;script type="text/javasript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt; function subdept(id) { &lt;?php subdepartment(id); ?&gt; } &lt;/script&gt; HTML Code: &lt;tr&gt; &lt;td&gt;Department&lt;sup style="color:red"&gt;*&lt;/sup&gt;-&lt;/td&gt; &lt;td&gt;&lt;?php department(); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Sub-department-&lt;/td&gt; &lt;td&gt;&lt;?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) ?&gt;&lt;/td&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload