Page 1 of 1

Select box - Update Form

Posted: Tue Apr 12, 2005 4:17 pm
by Addos
Hi,
I’m trying to return a value to a ‘select box’ in an ‘Update Form’ that has been previously stored in a field in my MSQL database. The bit of code I’m using is this:

Code: Select all

mysql_select_db($database_imtdatabase, $imtdatabase);
$query_rstSearchLocations = "SELECT * FROM locations ORDER BY Counties_IDPK ASC";
$rstSearchLocations = mysql_query($query_rstSearchLocations, $imtdatabase) or die(mysql_error());
$totalRows_rstSearchLocations = mysql_num_rows($rstSearchLocations);

<?php $strSavedValue = $row_getUpdateDetails['LocationList'];
	echo "<select name='LocationList' id='LocationList'>\n";
	while($dbRow = mysql_fetch_array($rstSearchLocations)){ 
  		echo "<option value='" . $dbRow["Counties_IDPK"]."'>";
		
	if ($dbRow["Counties_IDPK"] == $strSavedValue){
		echo 'Previous Selected'; // to be removed 
		 echo $dbRow["Counties_Name"]."</option>\n"; 
		} echo "</select>\n"; 
		}?>
Basically the ‘Form’ that initially passes this information is dynamically populated with a list of locations such as:

Cork town
Cork
Cork city

What I want to do is have the data that has been originally sent to the database returned to my Update Form with the specific selected value displayed in the select box but at the same time once the accompanying dropdown box is clicked will still reveal all the other data.

At the moment I’m getting this returned to the browser using the code above: http://ahamay.com/select.htm I think that my problem is to do with the use of ‘;’ etc but I’m really only at this PHP stuff since Christmas and struggle with the little things!

Thanks a mil for any help

Posted: Tue Apr 12, 2005 5:34 pm
by Burrito
the sample you posted only has an image of a select box....that I could see.

one thing I see in your code that might be killing it is you have your closing </select> inside your while loop which will print it out for every iteration of the loop.

you need to do something like this:

Code: Select all

<select name="myselect">
<?while($row = mysql_fetch_assoc($result)){ ?>
<option value="<? echo $result['whatever'];?>"><? echo $result['whatevershown'];?></option>
<? } // end your while loop before you close your select ?>
</select>
if you can post a link to the correct page that'll help us better help you...

Posted: Tue Apr 12, 2005 5:45 pm
by Burrito
jeezz...jsut realized I used result[]...doh! long day

Posted: Wed Apr 13, 2005 3:56 am
by Addos
Thanks very much for your help on this, it is most appreciated.
I have tried to set up a test. You can go to this page http://www.irishmusicteachers.ie/update_test_form.php? And make a selection and you will be sent to another page where you should see the same selection that has just been previously made with the option to update it but the second page only shows the first value on the list rather that the one selected.

This is the code from the first page (update_test) and the select box is dynamically filled from the database:

Code: Select all

<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
Choose County<?PHP echo "<select name='LocationList' id='LocationList'>\n";
while($dbRow = mysql_fetch_array($rstSearchLocations)){ 
echo "<option value='"
. $dbRow["Counties_IDPK"]  
. "'>"
. $dbRow["Counties_Name"]
."</option>\n"; 
} echo "</select>\n"; 
?>
<input name="sendCom" type="submit" id="sendCom" value="Post Details" />	
<input type="hidden" name="MM_insert" value="form1">
    </form>
This is the code from the second page:

Code: Select all

<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  Choose to update County from previous selection :	
<?php $strSavedValue = $row_getUpdateDetails['LocationList'];
	echo "<select name='LocationList' id='LocationList'>\n";
	while($dbRow = mysql_fetch_array($rstSearchLocations)){ 
  		echo "<option value='" . $dbRow["Counties_IDPK"]."'>";
		
	if ($dbRow["Counties_IDPK"] == $strSavedValue){
		//echo 'Previous Selected'; // to be removed 
		 echo $dbRow["Counties_Name"]."</option>\n"; 
		}} echo "</select>\n"; 
		?>
		
      <input type="submit" value="Update record"></td>
      <input type="hidden" name="MM_update" value="form1">
  <input type="hidden" name="Applicants_ID" value="<?php echo $row_getUpdateDetails['Applicants_ID']; ?>">
</form>
Thanks again for any advice I’m seem to be going around in circles with this.

Brian

Posted: Wed Apr 13, 2005 9:28 am
by Burrito
if you want the previous choice to be selected, you're going to need to check if the post value is the same as the value being looped from the db, if it is, just echo "selected=\"selected\"";