Note that there are some explanatory texts on larger screens.

plurals
  1. POFirst drop-down box in PHP with Javascript does not keep my selection
    text
    copied!<p>I am a beginner PHP programmer. I have managed to put together the following code, which creates a double drop-down list from a MySQL database. In the first box you are supposed to select a claims hub, and in the second box you are supposed to select an attorney, which reports to the hub in the first box.</p> <p>My problem is that it does not keep the selection of the first drop-down box, hence all the attorneys are available in the 2nd box. Can anybody tell me what I am doing wrong?</p> <p>Here is my code:</p> <pre><code>&lt;?php require_once('appvars.php'); require_once('connectvars.php'); require_once('header.php'); ?&gt; &lt;head&gt; &lt;script language=JavaScript&gt; function reload(form) { var selected_value=form.hub.options[form.hub.options.selectedIndex].value; self.location='registera.php?hub=' + selected_value ; } &lt;/script&gt; &lt;head&gt; &lt;?php @$hub=$HTTP_GET_VARS['hub']; if(strlen($hub) &gt; 0 and !is_numeric($hub)){ echo "INVALID DATA."; exit; } //This fetches content from CLAIMS HUBA table first to get the ATTORNEY list $hub_query=mysql_query("SELECT DISTINCT hub_name, hub_id FROM claims_huba order by hub_name"); if(isset($hub) and strlen($hub) &gt; 0) { echo "Loaded: Claims Hubs."; // If the numeric value of "hub" (selected attorney) is greater than zero we are OK // Then we will perform an SQL query to find all attorneys with that number. $attorney_query=mysql_query("SELECT DISTINCT firm_name FROM claims_hub where hub_id=$hub order by firm_name"); echo "Loaded: Attorney Firms by Hub ID."; }else { $attorney_query=mysql_query("SELECT DISTINCT firm_name FROM claims_hub order by firm_name"); echo "Loaded: Attorneys."; } echo "&lt;form method=post name=f1 action='register_ui.php'&gt;"; echo "&lt;select name='hub' onchange=\"reload(this.form)\"&gt;&lt;option value=''&gt;Please select the Claims Hub you are reporting to:&lt;/option&gt;"."&lt;BR&gt;"; while($qresult_hub = mysql_fetch_array($hub_query)) { if($qresult_hub['hub_id']==@$hub){echo "&lt;option selected value='$qresult_hub[hub_id]'&gt;$qresult_hub[hub_name]&lt;/option&gt;"."&lt;BR&gt;";} else{echo "&lt;option value='$qresult_hub[hub_id]'&gt;$qresult_hub[hub_name]&lt;/option&gt;";} } echo "&lt;/select&gt;"; echo "&lt;select name='attorney'&gt;&lt;option value=''&gt;Please select your firm: &lt;/option&gt;"; while($qresult_attorney = mysql_fetch_array($attorney_query)) { echo "Loaded: Attorneys queried."; echo "&lt;option value='$qresult_attorney[firm_name]'&gt;$qresult_attorney[firm_name]&lt;/option&gt;"; } echo "&lt;/select&gt;"; echo "&lt;input type=submit value=Submit&gt;"; echo "&lt;/form&gt;"; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload