cleme1q Posted February 11, 2010 Share Posted February 11, 2010 Hi, I have a case where I have 2 Drop Downs that are populated by a db query. When a choice is made in the first drop down, the page refreshes and the second drop down is correctly populated using the input from the first drop down. My problem is that when the page refreshes, the first drop down also refreshes so it no longer shows the selected option and goes back to a default setting. How can I have it show the selected setting on refresh. Here is the relevant snippet: <script language="JavaScript"> function autoSubmit() { var formObject = document.forms['CommentsForm']; formObject.submit(); } </script> <Select name=district onChange="autoSubmit();"> <option>Choose District</option> <?php //Select School district you want info on $district_query="select distinct district from school order by district"; $district_result=mysql_query($district_query); //$options_d=""; //*** loop over the results ***/ while ($row=mysql_fetch_array($district_result)) { $district=$row["district"]; $options_d.="<option value=\"$district\">".$district.'</option>'; } ?> <?php echo "$options_d" ?> </select> <br><br> <Select name="school_name_box"> <option value=0>Choose School <?php if(isset($_GET["district"])) { $district_selection = $_GET["district"]; } //Select School you want info on $school_query="select school_tag,school_name from school where district = \"$district_selection\" order by school_name"; $school_result=mysql_query($school_query); $options=""; //*** loop over the results ***/ while ($row=mysql_fetch_array($school_result)) { $tag=$row["school_tag"]; $school=$row["school_name"]; $options.="<option value=\"$tag\">".$school.'</option>'; } ?> <?php echo "$options" ?> </select> Link to comment https://forums.phpfreaks.com/topic/191829-dependent-drop-down-refreshes-first-drop-down/ Share on other sites More sharing options...
xjake88x Posted February 11, 2010 Share Posted February 11, 2010 Try something like this (read the comments in the php) <script language="JavaScript"> function autoSubmit() { var formObject = document.forms['CommentsForm']; formObject.submit(); } </script> <Select name=district onChange="autoSubmit();"> <option>Choose District</option> <?php //moved district_selection up here, because you need it $district_selection = isset($_GET["district"]) ? $_GET["district"] : false; //Select School district you want info on $district_query="select distinct district from school order by district"; $district_result=mysql_query($district_query); //$options_d=""; //*** loop over the results ***/ while ($row=mysql_fetch_array($district_result)) { $district=$row["district"]; $selected = $district_selection == $district ? 'selected': ''; $options_d.="<option value=\"$district\" $selected>".$district.'</option>'; } ?> <?php echo "$options_d" ?> </select> <br><br> <Select name="school_name_box"> <option value=0>Choose School <?php //moved the $district_selection variable to before the other select box //Select School you want info on $school_query="select school_tag,school_name from school where district = \"$district_selection\" order by school_name"; $school_result=mysql_query($school_query); $options=""; //*** loop over the results ***/ while ($row=mysql_fetch_array($school_result)) { $tag=$row["school_tag"]; $school=$row["school_name"]; $options.="<option value=\"$tag\">".$school.'</option>'; } ?> <?php echo "$options" ?> </select> And next time please put your code in tags Link to comment https://forums.phpfreaks.com/topic/191829-dependent-drop-down-refreshes-first-drop-down/#findComment-1011069 Share on other sites More sharing options...
cleme1q Posted February 12, 2010 Author Share Posted February 12, 2010 Thanks very much. It works now. I am new to programming in general and php in particular. I will try to use correct format when posting questions. Link to comment https://forums.phpfreaks.com/topic/191829-dependent-drop-down-refreshes-first-drop-down/#findComment-1011100 Share on other sites More sharing options...
xjake88x Posted February 12, 2010 Share Posted February 12, 2010 Glad I could help - how did you set your post as solved by the way? I haven't made threads I usually just reply to them. Link to comment https://forums.phpfreaks.com/topic/191829-dependent-drop-down-refreshes-first-drop-down/#findComment-1011103 Share on other sites More sharing options...
cleme1q Posted February 12, 2010 Author Share Posted February 12, 2010 In the bottom left hand corner of the page it has a button to allow me to MARK SOLVED. Link to comment https://forums.phpfreaks.com/topic/191829-dependent-drop-down-refreshes-first-drop-down/#findComment-1011104 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.