3

I have a simple inventory database and on the input_data form, one of the input fields is field, as follow:

<select>
    <option>In Inventory</option>
    <option>In Process</option>
    <option>Shipped/in-transit</option>
    <option>Received by recipient</option>
</select>

If for example I input a data with "In Process" and later I would like to update that perticular data. The default back to "In Inventory" on my update_page.php file. I would like to see the "In Process" value selected.

Here is a snippet from my non-working code from my update_page.php:

<select name="status" value="<?php echo $row['status']; ?>">
    <option>In Inventory</option>
    <option>In Process</option>
    <option>Shipped/in-transit</option>
    <option>Received by recipient</option>
</select>

4 Answers 4

6

Select has no value attribute in HTML - you can have multiple options selected, and this is determined by the selected attribute on the option element

Mucky way:

<select name="status">
    <option<?php if ($row['status'] == "In Inventory"): ?> selected="selected"<?php endif; ?>>In Inventory</option>
    <option<?php if ($row['status'] == "In Process"): ?> selected="selected"<?php endif; ?>>In Process</option>
    <option<?php if ($row['status'] == "Shipped/in-transit"): ?> selected="selected"<?php endif; ?>>Shipped/in-transit</option>
    <option<?php if ($row['status'] == "Received by recipient"): ?> selected="selected"<?php endif; ?>>Received by recipient</option>
</select>

Slightly better way:

 <?php
      $options = array("In Inventory", "In Process", "Shipped/in-transit", "Received by recipient");
 ?>

 <select>
     <?php foreach ($options as $option): ?>
         <option value="<?php echo $option; ?>"<?php if ($row['status'] == $option): ?> selected="selected"<?php endif; ?>>
             <?php echo $option; ?>
         </option>
     <?php endforeach; ?>
 </select>

Best way - store these options in a database table - probably better using ID values rather than strings (allows for easier updating of option labels) and loop over the possible options taking from a DB query like I have above. If this select list is used a lot, cache the results of the query to get the options (remember that you'd need to clear the cache if the list gets updated)

Sign up to request clarification or add additional context in comments.

Comments

0

Each option has its corresponding value not the select tag. So, you will have to compare the values of each option with the status.

Comments

0

You'll have to check for each one if it's the selected one, like this, for exampel:

<option <?php if ($row['status'] == 'In Process') echo 'selected'; ?>>In Process</option>

If you use jQuery, you could do a somewhat cleaner solution, in your case give your select tag an id, then:

$(document).ready(function() {
    $('#select-id option:contains("<?php echo $row['status']; ?>")').prop('selected', true);
});

2 Comments

That's not cleaner - that's complicating the issue by putting the logic in two different places
Yeah, fair enough. I disagree, but I see your point, and I realise it's not necessarily a good suggestion.
0

The simpler code I've found, using only JavaScript (no jQuery needed). Put it right after the declaration of the listbox and set the selected index there:

<script>
document.getElementById('listBoxId').selectedIndex=<?php echo $INDEX ?>;
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.