Skip to content
Advertisement

PHP selectfield get value from database, change and POST

I’m a beginner with PHP and have some decent issues ^^

Would like to pick the data from the field “disabled” from the database and update the selectfield with the value.

The selectfield has 2 possible options “yes” and “no” (value 0 or 1). Now i would finally like to post the selected or unchanged value back to the database.

Here all the values including disabled which are loaded into $row by MYSQL SELECT query

 $mydb->query('SELECT a.* ...
 $row = $mydb->fetchRow();

Here the working query to write into the database / table (tested on other fields). disabled is the field that should be updated/overwritten:

$sql = "REPLACE INTO `api_tokens` (`id`, `disabled`) VALUES (
                ".$id.",
                ".(empty($_POST['disabled']) ? 0 : 1).")";

Here the form which should have 2 selectable options, one of them shoudl be the value loaded from the database and the other value should be the opposite. Unclear value/ares is marked with ????

<div class="row">
    <label for="disabled">Deactivate</label>
    <select name="disabled" id="disabled">
        <option selected="<?=$row['disabled']?>"></option>
        <option value="????">No</option>
        <option value="????">Yes</option>
    </select>
                            
                     

Advertisement

Answer

Try this. The key is to check the value for each condition. If the saved choice was ‘no’ (0) then set the select attribute. Do the same for ‘yes’ (1).

<div class="row">
    <label for="disabled">Deactivate</label>
    <select name="disabled" id="disabled">
        <option value="0"<?php echo ($row['disabled'] == 0?' selected':''); ?>>No</option>
        <option value="1"<?php echo ($row['disabled'] == 1?' selected':''); ?>>Yes</option>
    </select>

UPDATE

I see from your comment that you’d like additional clarification. Here’s what happens:

  1. Each <option> element will specify a value attribute as one possible choice of the <select> element it belongs to. When a choice is made, the <option> element’s selected attribute is set. (Technically, there is the multiple attribute on the <select> element, but let’s not get into that now.)
  2. When your user submits the form, their choices (option values) are made persistent by storing them in a database. In this example, this will be either 1 for yes or 0 for no.
  3. If you want to present the user with the last state their form was in, you have to read their choices back from the database. You now have to set the select attribute on the <option> element they chose before. On their screen, their chosen option is now highlighted in the <select> list.

You can find the choice they submitted in $row['disabled'], where the 'disabled' key is equal to the name attribute of the <select> element. To set the select attribute on the right (previously chosen) <option> element, you check $row['disabled'] against each <option> element’s value. Are they equal? Then this was their last chosen option, so now highlight it and set the selected attribute on this specific <option> element.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement