| | |
I want to keep the drop down box value
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2008
Posts: 16
Reputation:
Solved Threads: 0
Hey,
My code i pretty messed up at the moment but i was hoping someone could help me change it so that when the form is reloaded the 2nd time round that the value in the drop down menu that's selected stays after the reload.
Thankyou,
Billy
My code i pretty messed up at the moment but i was hoping someone could help me change it so that when the form is reloaded the 2nd time round that the value in the drop down menu that's selected stays after the reload.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!doctype html public "-//w3c//dtd html 3.2//en"> <html> <head> <title>Postcode and Address Selection Test</title> <SCRIPT language=JavaScript> //Reloads the page function reload(form) { var val=form.postcode.value; self.location='dd.php?postcode=' + val; } function reload2(form) { var val=form.postcode.value; var val2=form.suburb.options[form.suburb.options.selectedIndex].value; self.location='dd.php?postcode=' + val + '&suburb=' + val2; } </script> </head> <body> <? @$postcode=$_GET['postcode']; // Use this line or below line if register_global is off @$suburb=$_GET['suburb']; /////// for suburb drop down list we will check if postcode is entered else we will display all the suburbs///// if(isset($postcode) and strlen($postcode) > 0){ $query=mysql_query("SELECT DISTINCT suburb FROM post2 where postcode=$postcode order by suburb"); //Table called POST2 } ////////// end of query for second subcategory drop down list box ///////////////////////////////////////////// echo "<form method=post name=f1 action='dd-check.php'>";//Goes to dd-check to check posted variables //Page Reloads after 4 digits are entered///<<<<--------------POSTCODE FIELD-------<<<<<<<<<<<<<< echo "Postcode: <input name='postcode' type=text onkeyup=\"if(this.value.length>3)reload(this.form);else return false;\" value=$postcode>"; echo"<input value='Load Suburbs' type='button' onclick=\"reload(this.form)\"> Click to Load Suburbs for this Postcode."; //////////////////Starting of suburb drop downlist///<<<<--------------SUBURB-------<<<<<<<<<<<<< echo"<br><br>"; echo"Suburb: "; echo "<select name='suburb' onchange=\"reload2(this.form)\"><option value='$suburb'>Select Suburb</option>"; // onchange=\"reload(this.form)\" while($pop = mysql_fetch_array($query)) { if($pop['postcode']==@$suburb){ echo "<option value='$suburb'>$pop[suburb]</option>"; }else{ echo "<option selected value='$suburb'>$pop[suburb]</option>";} } echo "</select>"; ////////////////// This will end the suburb drop down list /////////// //----------------------------------------------- echo "<br><br><input type=submit value=Submit>"; echo "</form>"; ?> </body>
Thankyou,
Billy
Last edited by BillyMako; Aug 9th, 2008 at 11:24 am.
•
•
Join Date: Aug 2008
Posts: 381
Reputation:
Solved Threads: 33
First, the form seems to be submitting as soon as I enter the fifth value in postcode? Not sure you want this behavior, but maybe you do, but it feels a little out of control to the user.
I think I would test when you are writing the <option> tags for the drop down if the item has been selected (submitted) before and do something like this...
Shouldn't you be using POST methods for form submission? Why try and use both?
Why are you using the $suburb value for all of your option values?
Anyway, what I've got here is a quick test to see if the previously posted value is the same as the value you are writing to the current drop down option -- if it is it populates $selected with the string ' selected', and if not it populates it with an empty string ''
I don't know for certain -- looking at your page code -- that $pop['postcode'] is the correct value to compare to the option values, but something will be and then this should work.
Good luck
I think I would test when you are writing the <option> tags for the drop down if the item has been selected (submitted) before and do something like this...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<?php @suburb = $_POST['suburb']; ... more code goes here ... while($pop = mysql_fetch_array($query)) { $selected = ( $pop['postcode'] == $suburb ) ? ' selected' : ''; echo "<option value=\"$suburb\"$selected>$pop[suburb]</option>"; } ?>
Why are you using the $suburb value for all of your option values?
Anyway, what I've got here is a quick test to see if the previously posted value is the same as the value you are writing to the current drop down option -- if it is it populates $selected with the string ' selected', and if not it populates it with an empty string ''
I don't know for certain -- looking at your page code -- that $pop['postcode'] is the correct value to compare to the option values, but something will be and then this should work.
Good luck
Last edited by langsor; Aug 11th, 2008 at 4:46 am.
![]() |
Similar Threads
- assign database value as selected value in a drop down box (PHP)
- resultset from drop down box (JSP)
- Content Changes across pages via Drop-Down Box (ASP.NET)
- Drop down box won't shut off video on reverse in IE (JavaScript / DHTML / AJAX)
- Drop down box not working (ASP.NET)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Filtered search function on a website
- Next Thread: How to replace the div content onclick?
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column createrange() css cursor debugger dependent disablefirebug dom download dropdown editor element embed engine error events explorer ext file form forms getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl math media microsoft mimic object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post problem programming progressbar regex runtime scroll search security select session shopping size software sql text textarea unicode w3c web website window windowofwords windowsxp wysiwyg \n





