Using HTML and PHP, I am selecting and displaying records from a mysql db on a page. When I click one record, it opens an edit window. This new window displays all the content for one record selected and all content can be updated. Updates are then saved to the db for this record.
My problem is this: One of the fields is displayed in the edit window as a dropdown list. The dropdown is filled using a select on the page (similar to below). When I click a particular record to open the edit window, I always see the first option in the drop-down, not the option that is tied to the record.
I need to be able to see the option from the dropdown that is connected to the record I am editing.
For example, if I open a record to edit it and the value as saved in the table is "3", I want "3" to appear in the dropdown. I am always seeing the first option no matter what the actual value saved to the table is, or "1" as in the example select below. Can someone give me some pointers please? I would like to do this possibly using jquery.
<select name="itemst" id="itemst">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
The select is getting the necessary fields from the tables where i.item_description = d.id and i.id = '".$_GET['id']."'";
This works fine. If I want to separately display the content of the field for a particular record/row which is connected to this drop-down, I can. For example, echo $r_item['item_status']. This works fine. I just need to be able to have this value from the db display when the edit form opens and shows the dropdown.
I got it to work using the following. Thanks for the suggestions.
$status = $r_item['item_status'];
<select id="item_status" name="item_status">
<option value="" <?php if (!empty($status) && $status == '' ) echo 'selected = "selected"'; ?>></option>
<option value="1" <?php if (!empty($status) && $status == '1') echo 'selected = "selected"'; ?>>1</option>
<option value="2" <?php if (!empty($status) && $status == '2') echo 'selected = "selected"'; ?>>2</option>
<option value="3" <?php if (!empty($status) && $status == '3') echo 'selected = "selected"'; ?>>3</option>
</select>
Another solution
The above works, but I have since updated it with the following:
$status = $r_item['item_status'];
<select name="item_status" id="item_status">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
With the following line of JQuery:
$('#item_status').val('<?=$r_item['item_status'];?>');
And included the jquery reference in my header.