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.

<!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

Member Avatar for langsor

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...

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

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 is the correct value to compare to the option values, but something will be and then this should work.

Good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.