Skip to content
Advertisement

correct way of saving dropdown in php

I am using 2 kinds of dropdowns. The Yes/No and one with multiple values. Either way I use this:

<select id="active">
    <option value="0">No</option>
    <option value="1">Yes</option>
</select>

or

<select id="active">
    <option value="1">Choice 1</option>
    <option value="2">Choice 2</option>
    <option value="3">Choice 3</option>
    <option value="4">Choice 4</option>
</select>

The value of the Yes/No I store as a bit (before as tinyint) in my mysql-db. The multiple-choice I store as int. I get the values out of my object $member and try to select the right value:

<option value="0" <?php echo $member->active == 0 ? 'selected="selected"' : 'selected="unselected"'; ?>>Yes</option>
<option value="1" <?php echo $member->active == 1 ? 'selected="selected"' : 'selected="unselected"'; ?>>Yes</option>

This works perfectly when I have the multiple choice stored as int but not with the Yes/No stored as bit or tinyint.

What am I doing wrong? The value of $member->active are right. I know Yes/No can be done with a checkbox but I won’t this sorted out first.

Thx

Advertisement

Answer

To provide an answer and assuming you are already loading the appropriate data and storing it into whatever class/object $member is.

<option value="0" <?php echo $member->active == 0 ? 'selected="selected"' : ''; ?>>No</option>
<option value="1" <?php echo $member->active == 1 ? 'selected="selected"' : ''; ?>>Yes</option>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement