Page 1 of 1

Holding a value in a select box

Posted: Sun Aug 21, 2005 11:38 am
by Addos
I have a form where values are retained in the form fields using:

Code: Select all

<input type="text" name="fullname" value="<?php if (isset($_POST['fullname'])) echo $_POST['fullname'];?>" size="32">
This is used so that then any conditional statements throw and error any of the correctly entered fields will hold their content.

I also then have a dynamic select box

Code: Select all

<?PHP echo "<select name='bedrooms' id='bedrooms'>\n
	  <option value='%'>Search All</option>"; 
		while($dbRow = mysql_fetch_array($getBedrooms)){ 
  		echo "<option value='"
		. $dbRow["bedrooms_details"]  
		. "'>"
		. $dbRow["bedrooms_details"]
		."</option>\n"; 
		} echo "</select>\n"; 
		?>


I have no real idea as to how to apply the same PHP code to hold a value that has also been selected. Can anyone fill me in on how this is done? :roll:
Thanks very much

Posted: Sun Aug 21, 2005 12:33 pm
by anjanesh

Code: Select all

<?PHP echo "<select name='bedrooms' id='bedrooms'>\n
      <option value='%'>Search All</option>";
        while($dbRow = mysql_fetch_array($getBedrooms)){
          echo "<option value='"
        . $dbRow["bedrooms_details"]
        . "'"
        . (isset($_POST['bedrooms']) ? $_POST['bedrooms'] ==  $dbRow["bedrooms_details"] ? "selected='selected'" : "" : "")
        . ">"
        . $dbRow["bedrooms_details"]
        ."</option>\n";
        } echo "</select>\n";
        ?>