Jump to content

php array elements and javascript?


ann

Recommended Posts

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;
}

 

Link to comment
https://forums.phpfreaks.com/topic/169581-php-array-elements-and-javascript/
Share on other sites

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.