Jump to content

Javascript/PHP Form


Usarjjaco
 Share

Recommended Posts

Hello Everyone,Been awhile since I've been around but I'm back again, this time hoping to get some insight from the Javascript Guru's out there. I'm just starting to get into it and I have a decent grasp on it but this most recent roadblock has me at a loss... so I'm calling in some back up.The Concept: Multiple Drop down form, with each drop down dynamically altering what the next one will show using PHP. This I have been able to do from drop down 1 to drop down 2, with a successful page reload using the onselect function. However when I try to run the same function (with a different name of course) on the second menu to populate the third, it doesn't perform the page refresh.. here's the code.. hoping someone will spot my error!Head Section:

<script language=JavaScript>function reload(form){var val=form.make.options[form.make.options.selectedIndex].value;self.location='newsearch.php?make=' + val; }function reload3(form){var val=form.make.options[form.make.options.selectedIndex].value;var val2=form.model.options[form.model.options.selectedIndex].value;self.location='newsearch.php?make=' + val '&model=' + val2; }</script>

Body & Form

<?php$brand=mysql_query("SELECT DISTINCT Make FROM dealerfeed WHERE VehicleType!='1' ORDER BY Make ASC");$make=$_GET['make'];$model=$_GET['model'];if($make!='AllMakes'){$makecheck=mysql_query("SELECT DISTINCT Model FROM dealerfeed WHERE Make='$make' AND VehicleType!='1' ORDER BY Model ASC");}else{$makecheck=mysql_query("SELECT DISTINCT Model FROM dealerfeed WHERE VehicleType!='1' ORDER BY Model ASC"); }if($model!='AllModels'){$trimcheck=mysql_query("SELECT DISTINCT Trim FROM dealerfeed WHERE Model='$model' AND VehicleType!='1' ORDER BY Trim ASC");}else{$trimcheck=mysql_query("SELECT DISTINCT Trim FROM dealerfeed WHERE VehicleType!='1' AND Make='$make' ORDER BY Trim ASC"); }echo "<form method=post name=f1 action='vehiclesearch.php'>";echo "<select name='make' onchange=\"reload(this.form)\"><option value='AllMakes'>All Makes</option>";while($makesearch = mysql_fetch_array($brand)) {if($makesearch['Make']==@$make){echo "<option selected value='$makesearch[Make]'>$makesearch[Make]</option>"."<BR>";}else{echo "<option value='$makesearch[Make]'>$makesearch[Make]</option>";}}echo "</select><br><br>";echo "<select name='model' onchange=\"reload3(this.form)\"><option value='AllModels'>AllModels</option>";while($modelsearch = mysql_fetch_array($makecheck)) { if($modelsearch['Model']==@$Model){echo "<option selected value='$modelsearch[Model]'>$modelsearch[Model]</option>"."<BR>";}else{echo  "<option value='$modelsearch[Model]'>$modelsearch[Model]</option>";}}echo "</select><br><br>";echo "<select name='trim'><option value='AllTrims'>All Trims</option>";while($trimsearch = mysql_fetch_array($trimcheck)) {echo "<option value='$trimsearch[Trim]'>$trimsearch[Trim]</option>";}echo "</select>";echo "<input type=submit value=Search Inventory>";echo "</form>";?>

From the above code the first menu populates correctly. Once you select the first menu the second one populates correctly after the automatic refresh. However when I select the second drop down option it doesn't refresh nor does it populate the third menu. Thanks in advance!

Link to comment
Share on other sites

If it really is a JavaScript problem, looking at all that PHP could be misleading. Have you viewed the source that gets sent to the browser? The problem will almost certainly turn up there. If you can't see it, maybe post THAT to us.Better yet, post a link, if you can.

Link to comment
Share on other sites

Deirdre.Will work on a link. The problem is almost certainly in the Javascript. In My guess of narrowing it down it seems like the second onchange located here:

echo "<select name='model' onchange=\"reload3(this.form)\"><option value='AllModels'>AllModels</option>";while($modelsearch = mysql_fetch_array($makecheck)) {if($modelsearch['Model']==@$Model){echo "<option selected value='$modelsearch[Model]'>$modelsearch[Model]</option>"."<BR>";}else{echo  "<option value='$modelsearch[Model]'>$modelsearch[Model]</option>";}}echo "</select><br><br>";

hmm... Also an interesting addendum .. Just tried something... When the code in the head is formatted like this :

<script language=JavaScript>function reload(form){var val=form.make.options[form.make.options.selectedIndex].value;self.location='newsearch.php?make=' + val; }function reload3(form){var val=form.make.options[form.make.options.selectedIndex].value;var val2=form.model.options[form.model.options.selectedIndex].value;self.location='newsearch.php?make=' + val '&model=' + val2; }</script>

None of the drop down menu's will function with the onchangeWhen the code in the head section is placed as such...

<script language=JavaScript>function reload(form){var val=form.make.options[form.make.options.selectedIndex].value;self.location='newsearch.php?make=' + val; }</script><script language=JavaScript>function reload3(form){var val=form.make.options[form.make.options.selectedIndex].value;var val2=form.model.options[form.model.options.selectedIndex].value;self.location='newsearch.php?make=' + val '&model=' + val2; }</script>

The first drop down will function, but the second will not. (The second function is the reload3) ... So basically when it's function is in it's own script tag, the first portion functions. If they are both in the same script tags, not even the first one will function.. not sure if this helps one of you guys know what the issue could be...It almost seems like (And hopefully this makes sense to someone with more JavaScript knowledge... ) when the second onchange happens it just isn't reloading the page. Which makes me believe the onchange isn't fully functioning to call the function in the head portion.

Edited by usarjjaco
Link to comment
Share on other sites

I just caught the missing + in this line. You would have seen it too if you checked your console. :)
self.location='newsearch.php?make=' + val '&model=' + val2;

Many thanks! It's always the little ones eh? ... My apologies for bringing such a petty thread to light... I suppose I had just stared at it too long :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...