0

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>
4
  • What is your HTML code? Commented Nov 23, 2013 at 8:49
  • Your are calling server side function directly in client side you need an ajax call Commented Nov 23, 2013 at 8:50
  • @koljanep added the html code. its inside a table format. btw in the html code when i added sql query it is working but to some extent. now only the first field is visible for sub department Commented Nov 23, 2013 at 8:59
  • @dianuj well i'm not much comfortable with ajax. so trying to do with simple javascript and php. Commented Nov 23, 2013 at 9:01

1 Answer 1

3

A PHP webpage is generated in the following way:

  • The PHP code runs on the server, possibly inserting some text into the HTML or Javascript.
  • The resulting page, stripped of its PHP, is sent from the server to the client.
  • The client displays the page.

So when a user sees the page, there is no PHP code left to run. Even if there were, it wouldn't matter, because the client does not have access to the server. How do you get more data from the server? You either have to load another page, or you have to use AJAX.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the explanation. i think i'll have to learn ajax then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.