ann Posted August 10, 2009 Share Posted August 10, 2009 Hi I've got a page with a number of dependent dropdown menus. i.e. using car manufacturer/models as an example...If the user selects 'Ford' as a manufacturer javascript reloads the page with ?manufacturer=Ford in the url and php restricts the model dropdown to models made by Ford. It works the other way too, if they select a model first the list of manufacturers is restricted. Things just got more complicated and users may select up to 3 models from the same manufacturer. If I turn the form element 'model' into an array how do I get javascript to work with it? I just need what to to with the call to the reload() function and the javascript. I can deal with php arrays. Many thanks Bits of my code for a single model... html <select name="make" onchange="reload(this.form,'select.php')" > <option value="0" selected></option> <option value="1">Ford</option> <option value="2">VW</option> <option value="4">Opal</option> </select> <select name="model" onchange="reload(this.form,'select.php')" > <option value="0" selected></option> <option value="4">fiesta</option> <option value="23">fabia</option> <option value="24">td9</option> </select> php function ddmodel(){ #model if (isset($_REQUEST['make']) and $_REQUEST['make']>0) { $mainquery="SELECT DISTINCT id,model FROM model where make=".$_REQUEST['make'] } else {$mainquery="SELECT id,model from model"; } $string= "<td><select name=\"model[".$n."]\" onchange=\"reload(this.form,'".$_SERVER['PHP_SELF']."')\" >"; $string.= "<option value=\"0\" selected></option>\n"; $ddsql_result = mysql_query($mainquery); if(mysql_num_rows($ddsql_result)){ while($ddrow = mysql_fetch_assoc($ddsql_result)){ if ($_REQUEST['model']==$ddrow['id']) { $string.= "<option value=\"".$ddrow['id']."\" selected>".$ddrow['model']."</option>\n"; } else {$string.= "<option value=\"".$ddrow['id']."\">".$ddrow['model']."</option>\n";} }} $string.= "</select></td>\n"; return $string; } function ddmake(){ #make if (isset($_REQUEST['model']) and $_REQUEST['model']>0) { $mainquery="SELECT make.make,make.id from make left join model on (make.id=model.make) where model.id=".$_REQUEST['model']; } else {$mainquery="SELECT make,id from make order by make"; } $string= "<td><select name=\"make\" onchange=\"reload(this.form,'".$_SERVER['PHP_SELF']."')\" >"; $string= "<option value=\"\"selected></option>\n"; $ddsql_result = mysql_query("$mainquery"); if(mysql_num_rows($ddsql_result)){while($ddrow = mysql_fetch_assoc($ddsql_result)){ if ($_REQUEST['make']==$ddrow['id']) { $string.= "<option value=\"".$ddrow['id']."\" selected>".$ddrow['make']."</option>\n"; } else { $string.= "<option value=\"".$ddrow['id']."\">".$ddrow['make']."</option>\n"; } }} $string.= "</select></td>\n"; return $string; } javascript function reload(form,selfid){ var str='?'; if (form.make && form.make.options[form.make.options.selectedIndex].value>0) { str +='&make=' + form.make.options[form.make.options.selectedIndex].value; } if (form.model && form.model.options[form.model.options.selectedIndex].value>0) { str +='&model=' + form.model.options[form.model.options.selectedIndex].value; } self.location=selfid + str; } Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted August 10, 2009 Share Posted August 10, 2009 If it uses javascript might as well use Ajax and only refresh the pulldown that is connected. Try looking up "javascript chained select". There are also a couple of examples of that on this forum Quote Link to comment 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.