Log in

View Full Version : getting selected value of a drop down from db



mulaus
09-11-2009, 01:34 PM
i would like my user to be able to edit a form

how do i get the selected value of a drop down from db




<select name='room'>

<option value=''>-</option>
<option value='Room A'>Room A</option>
<option value='Room B'>Room B</option>
</select>

forum_amnesiac
09-11-2009, 02:11 PM
A lot depends on what database you are using.

You would do database search on the value of the HTML field 'room'.

If in your <form> you had 'action="POST" ' you would set that to a PHP variable like this.


$room_type=$_POST['room'];

If in the database you had a field called 'roomtype' you would then do a database search for all instances where this field matched the variable $room_type.

Sorry if this is a bit vague but you didn't give a lot of information in your question

mulaus
09-11-2009, 02:17 PM
I'm using mysql/php



$result = mysql_query("SELECT * FROM class WHERE log_id=$log_id");
while($row = mysql_fetch_array($result))
{
$log_id=$row['log_id'];
$room=$row['room'];
}

<form method='post' action='update.php'>

<select name='room'>

<option value=''>-</option>
<option value='Room A'>Room A</option>
<option value='Room B'>Room B</option>
</select>

</form>

forum_amnesiac
09-11-2009, 03:15 PM
inside the file update.php you would have

$room_type=$_POST['room'];

You would also include the code to open the database

As I said before, I'll assume you have a field for the room type called 'roomtype' in Table 'rooms'.

You would search the table like this


$result = mysql_query("SELECT * FROM rooms WHERE roomtype=$room_type");

Hope this helps

mulaus
09-11-2009, 04:45 PM
thanks for the suggestion fa

i finally got it...i made it simple..i don't want to use any extra table

i don't know if this is the correct way but it works
:-)




<select name='room'>

if($room== 'Room A') {
<option value='Room A' selected>Room A</option>
<option value='Room B'>Room B</option>
}

else if ($room== 'Room B')
{
<option value='Room A'>Room A</option>
<option value='Room B' selected>Room B</option>

}

else
{
<option value='Room A'>Room A</option>
<option value='Room B'>Room B</option>
}
</select>