Jump to content

Recommended Posts

Hi, I have a form with dropdowns which are populated from a mysql database. I need them to work so that the selection in the first dropdown narrows the mysql query and therefore the results in the second drop down, I of course need this to happen when different options are selected in the first drop down. Here is my code so far:

 

<label>Department</label><br />   
<select name="department" size="1">
<?php 
$department_query = $local_mysqli->query("SELECT id, name FROM department");
while ($department_array = mysqli_fetch_array($department_query)){
$department_id = $department_array['id'];
$department_name = $department_array['name'];
echo "<option value=\"$department_id\">$department_name</option>\n";
};
?>
</select>
<br />
<label>Position</label><br />   
<select name="position" size="1">
<?php 
$position_query = $local_mysqli->query("SELECT id, title FROM positions WHERE assigned=0, department=$department_id");
while ($position_array = mysqli_fetch_array($position_query)){
$position_id = $position_array['id'];
$position_title = $position_array['title'];
echo "<option value=\"$position_id\">$position_title</option>\n";
};
?>
</select>

Link to comment
https://forums.phpfreaks.com/topic/160451-solved-dropdowns/
Share on other sites

You will most probably have to do this using Ajax or else loading all the data for positions into a very large javascript array which you then manipulate depending on the input of your 1st box.

You cannot do it as above, because all the PHP will be parsed before it is output to the client screen. i.e. they won't have selected the option before the 2nd SQL statement is run.

 

The other option (without javascript) is to onselect for the department "POST" all the current form data to your page, and re-render it populating the 1st box with the selected option, and building the 2nd box from the posted values.

Link to comment
https://forums.phpfreaks.com/topic/160451-solved-dropdowns/#findComment-846732
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.