Jump to content

Dependent Drop Down refreshes first Drop Down


cleme1q

Recommended Posts

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>

 

 

 

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

Archived

This topic is now archived and is closed to further replies.

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