Click to See Complete Forum and Search --> : Select default option in dynamic drop down menu


mfaulkner
01-27-2007, 01:31 PM
Hi all, I'm having abit of trouble working out how to automatically default to the previous option entered into MySQL via an HTML form. Its updating the database, not inserting new vales, so its vital that when loading the update page the stored values are selected by default. Heres the form code for what I've done so far:

<td class="<?php echo $jan_1; ?>">1. <select name="jan_1" size="1">
<option value="Available">Available</option>
<option value="Booked">Booked</option>
</select>
</td>

Any help would be really grateful. Thanks in advance,

Mark.

NightShift58
01-27-2007, 01:54 PM
You don't say what the field is called, so I'll give it a name: $row['jan_1'].<td class="<?php echo $jan_1; ?>">1. <select name="jan_1" size="1">
<option value="Available" <?php echo ($row['jan_1']=="Available" ? 'selected="selected"' : ''); ?>>Available</option>
<option value="Booked" <?php echo ($row['jan_1']=="Booked" ? 'selected="selected"' : ''); ?>>Booked</option>
</select>
</td>

mfaulkner
01-28-2007, 10:37 AM
NightShift58, thanks for your reply. I tried the code you suggested but its still not displaying the current value as the default option. I'll provide more information in any case, maybe the problem will become clearer then.

The database is structured as follows:

FIELD TYPE
jan_1 enum('Available', 'Booked')
jan_2 enum('Available', 'Booked')
etc...

Also, I'm not really too sure what your suggested code actually does.

Thanks in advance guys,

Mark.

NightShift58
01-28-2007, 10:48 AM
Sorry... enum is an integer value and not a string. Try this:<td class="<?php echo $jan_1; ?>">1. <select name="jan_1" size="1">
<option value="1" <?php echo ($row['jan_1'] == "1" ? 'selected="selected"' : ''); ?>>Available</option>
<option value="2" <?php echo ($row['jan_1'] == "2" ? 'selected="selected"' : ''); ?>>Booked</option>
</select>
</td> This is supposed to highlight the item previously chosen by the user - actually highlight the option store in the tabel.

mfaulkner
01-28-2007, 03:30 PM
Thanks again for your reply, but still no luck I'm afraid.


<?php
$retrievejan = mysql_query("SELECT * FROM january")
or die(mysql_error());
while ($rows = mysql_fetch_array($retrievejan)) {
extract($rows);
}
?>

Thats the code I'm using to retrieve all the data from the database and I've tried the modification you suggested. My form is updating the database and the data is also being displayed correctly. Really stumped now :s

NightShift58
01-28-2007, 05:44 PM
You don't say what the field is called, so I'll give it a name: $row['jan_1'].<td class="<?php echo $jan_1; ?>">1. <select name="jan_1" size="1">
<option value="Available" <?php echo ($row['jan_1']=="Available" ? 'selected="selected"' : ''); ?>>Available</option>
<option value="Booked" <?php echo ($row['jan_1']=="Booked" ? 'selected="selected"' : ''); ?>>Booked</option>
</select>
</td>I told you I didn't know the name of your field. I used that name for example purposes. You'll have to rename it to match your field name. I doubt I guessed it right and if I did, I'll start palying the lottery...

mfaulkner
01-28-2007, 06:22 PM
lol, but you guessed right, it was called 'jan_1'. thanks for your help anyhow, I will investigate it more tomorrow.

mfaulkner
01-29-2007, 01:43 PM
Still no luck, this is the code I'm using and I've attached a screenshot of my database structure. :( I've tried lots of different combinations but cannot see why it isnt working.

<?php
require_once('../includes/dbconnect.php');

$result = mysql_query("SELECT * FROM january")
or die(mysql_error());

$row = mysql_fetch_array($result)
or die(mysql_error());
?>

<form>

<select name="jan_1" size="1">
<option value="1" <?php echo ($row['status'] == "1" ? 'selected="selected"' : ''); ?>>Available</option>
<option value="2" <?php echo ($row['status'] == "2" ? 'selected="selected"' : ''); ?>>Booked</option>
</select>

</form>

mfaulkner
02-10-2007, 07:10 AM
Problem Solved:

Heres the code which eventually answered my question:

<select name="jan_status_1" size="1">
<option value="Available"<?php echo ($row_1['jan_status'] == 'Available') ? ' selected' : '' ?>>Available</option>
<option value="Booked"<?php echo ($row_1['jan_status'] == 'Booked') ? ' selected' : '' ?>>Booked</option>
</select>

I understand how the above shortened IF statements work now, thanks!