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
Reply

Join Date: Jan 2008
Posts: 16
Reputation: BillyMako is an unknown quantity at this point 
Solved Threads: 0
BillyMako BillyMako is offline Offline
Newbie Poster

I want to keep the drop down box value

 
0
  #1
Aug 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: I want to keep the drop down box value

 
0
  #2
Aug 11th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4199 | Replies: 1
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC