Boolean - Edit field - Dynamically populated dropdown box

I have this code for an edit field (Does room have air conditioning - 1=yes,0=no). It displays a boolean value of 0 or 1.

I would like to rather have the user able to change the variable by use of perhaps a dropdown box, with the default value being the current value that is given to the record field(0 or 1).

BTW here is the coding for the initial input of a new record using the field (using dropdown box):

                <select name="Air">
                  <option value="0">No</option>
                  <option value="1">Yes</option>
                </select>Air Conditioning

Could I please have help with this one, so that a result can be achieved using preferably dropdown boxes… Thankyou!

Hi joh,

Try something like this:

[php]

<?php

switch ($row_Recordset1[‘Air’])
{

case true:
$select1 = “selected”;
break;

case false:
$select0 = “selected”;
break;
}
?>

value="0">No value="1">Yes Air Conditioning

[/php]

The switch statement checks the current state of your $row_Recordset1[‘Air’] field, and assigns the relevant variable which will then display the corresponding option value as selected.

I also added some javascript, which will automatically make the drop down menu work once the other option is chosen. You would then need to create some code to change the variable.

Think that should work ok.

:)

Hey thanks heaps Carella for that! I tried it out, and after some experimentation (variable declaration) got a working result:

<select name="Air" onChange="location=this.options[this.selectedIndex].value"> 
							<?php 
							$select0 = "";
							$select1 = ""; 

							switch ($row_Recordset1['Air']) 
							{ 
	
							  case false: 
							 $select0 = "selected"; 
							 break; 
	   
						   case true: 
						   $select1 = "selected"; 
						   break; 
							} 
							?>  
							<option <?php echo $select0; ?> value="0">No</option> 
							<option <?php echo $select1; ?> value="1">Yes</option> 
							</select>Air Conditioning 

However, there is one problem; Although the variables do change when yes or no is selected (0 or 1), the dropdown box then automatically sends me to a new address ‘0’, or ‘1’, which do not exist as files. How do I fix that so that the dropdown does not send me anywhere?

Hi Joh,

Just remove the following from the code:

onChange="location=this.options[this.selectedIndex].value

Thanks a million Carella, you are a brilliant lifesaver!!!

LOL! Well, we are all here to help each other. Thats what this forum is for.

:)
Sponsor our Newsletter | Privacy Policy | Terms of Service