943,587 Members | Top Members by Rank

Ad:
Aug 9th, 2008
0

I want to keep the drop down box value

Expand Post »
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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!doctype html public "-//w3c//dtd html 3.2//en">
  2.  
  3. <html>
  4.  
  5. <head>
  6.  
  7. <title>Postcode and Address Selection Test</title>
  8.  
  9. <SCRIPT language=JavaScript> //Reloads the page
  10. function reload(form)
  11. {
  12. var val=form.postcode.value;
  13. self.location='dd.php?postcode=' + val;
  14. }
  15.  
  16. function reload2(form)
  17. {
  18. var val=form.postcode.value;
  19. var val2=form.suburb.options[form.suburb.options.selectedIndex].value;
  20. self.location='dd.php?postcode=' + val + '&suburb=' + val2;
  21. }
  22.  
  23. </script>
  24. </head>
  25.  
  26. <body>
  27. <?
  28. @$postcode=$_GET['postcode']; // Use this line or below line if register_global is off
  29. @$suburb=$_GET['suburb'];
  30.  
  31.  
  32. /////// for suburb drop down list we will check if postcode is entered else we will display all the suburbs/////
  33. if(isset($postcode) and strlen($postcode) > 0){
  34. $query=mysql_query("SELECT DISTINCT suburb FROM post2 where postcode=$postcode order by suburb"); //Table called POST2
  35. }
  36.  
  37. ////////// end of query for second subcategory drop down list box /////////////////////////////////////////////
  38.  
  39. echo "<form method=post name=f1 action='dd-check.php'>";//Goes to dd-check to check posted variables
  40.  
  41. //Page Reloads after 4 digits are entered///<<<<--------------POSTCODE FIELD-------<<<<<<<<<<<<<<
  42. echo "Postcode: <input name='postcode' type=text onkeyup=\"if(this.value.length>3)reload(this.form);else return false;\" value=$postcode>";
  43. echo"<input value='Load Suburbs' type='button' onclick=\"reload(this.form)\"> Click to Load Suburbs for this Postcode.";
  44.  
  45. //////////////////Starting of suburb drop downlist///<<<<--------------SUBURB-------<<<<<<<<<<<<<
  46. echo"<br><br>";
  47. echo"Suburb: ";
  48. echo "<select name='suburb' onchange=\"reload2(this.form)\"><option value='$suburb'>Select Suburb</option>"; // onchange=\"reload(this.form)\"
  49. while($pop = mysql_fetch_array($query)) {
  50. if($pop['postcode']==@$suburb){
  51. echo "<option value='$suburb'>$pop[suburb]</option>";
  52. }else{
  53. echo "<option selected value='$suburb'>$pop[suburb]</option>";}
  54. }
  55.  
  56. echo "</select>";
  57. ////////////////// This will end the suburb drop down list ///////////
  58.  
  59.  
  60. //-----------------------------------------------
  61.  
  62. echo "<br><br><input type=submit value=Submit>";
  63. echo "</form>";
  64. ?>
  65. </body>

Thankyou,
Billy
Last edited by BillyMako; Aug 9th, 2008 at 11:24 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BillyMako is offline Offline
16 posts
since Jan 2008
Aug 11th, 2008
0

Re: I want to keep the drop down box value

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...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. @suburb = $_POST['suburb'];
  3.  
  4. ... more code goes here ...
  5.  
  6. while($pop = mysql_fetch_array($query)) {
  7. $selected = ( $pop['postcode'] == $suburb ) ? ' selected' : '';
  8. echo "<option value=\"$suburb\"$selected>$pop[suburb]</option>";
  9. }
  10. ?>
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
Last edited by langsor; Aug 11th, 2008 at 4:46 am.
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Filtered search function on a website
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How to replace the div content onclick?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC