rhodrykorb Posted June 1, 2009 Share Posted June 1, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/160451-solved-dropdowns/ Share on other sites More sharing options...
aschk Posted June 1, 2009 Share Posted June 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160451-solved-dropdowns/#findComment-846732 Share on other sites More sharing options...
rhodrykorb Posted June 1, 2009 Author Share Posted June 1, 2009 Has anyone done this before and have sample code for me?? Quote Link to comment https://forums.phpfreaks.com/topic/160451-solved-dropdowns/#findComment-846734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.