Home > database >  How can I set the default value for an HTML <select> element if I don't know it?
How can I set the default value for an HTML <select> element if I don't know it?

Time:10-19

I currently display all the row of my table as form, so the information can directly be modify. Event thought it's a form, all the data are pre-filled in the value field of my input. There is one exeption, a parameter of my form which is not an input but a select. No matter what "Fiscalité IS" is dislay witch correspond the the option value 1. I try to solve the problem by adding a selected field who take the categories of my table's row as parameter like this:

<select name="categorie" id="cat-select" selected="<?php echo $rows['categorie']; ?>">

I know that I'm supposed to use selected in the option, it's just to show my research. The tricky thing, I don't know in advance witch one should be select. It's depend on a the data in my table, and it's variable in each turn of my loop.

Here is my complete form in my code:

    <?php
                    while ($rows = $result-> fetch_assoc()) {
                        ?>
                        <div >
                            <?php   
                                echo $rows['id'] .  " <br> " .  " <br> ";
                            ?>
                           <input type=hidden name="rowid" value="<?php echo $rows['id']; ?>">
                           <p>nom:</p>
                            <input type="text" name="nom" minLength="9" maxLength="15" value="<?php echo $rows['nom'];?>">
                            <p>catégorie:</p>
                            <select name="categorie" id="cat-select" selected="<?php echo $rows['categorie']; ?>">
                                <option value="1">Fiscalité IS</option>
                                <option value="2">Réorganisations, acquisitions et méthodologie</option>
                                <option value="3">Contrôle et contentieux fiscal</option>
                                <option value="4">Fiscalité patrimoniale</option>
                                <option value="5">Fiscalité Immobilière</option>
                                <option value="6">Introduction à la TVA et droits de douanes</option>
                                <option value="7">Excel</option>
                            </select>
                            <p>url:</p>   
                            <input type="tel" name="url" minLength="3" maxLength="15" value="<?php echo $rows['url'];?>">
                            <p>img:</p>
                            <input type="tel" name="img" minLength="9" maxLength="15" value="<?php echo $rows['img'];?>">
                            <br><br>
                            <button  id="<?php echo $rows['id']; ?>" >Modifier</button>
                        <button  id="<?php echo $rows['id']; ?>" onclick ="deletedata(this.id)">Supprimer</button>
                </div>
                    <?php
                }
            ?>

Here are the fiel of my table:

| id  | nom                                    | categorie | url                                                                           | direct_redirect | img   

CodePudding user response:

If $rows['categorie'] contains a number from 1 to 7 you can do this with your select:

<?php

$rows['categorie'] = 6;

$options = [1 => "Fiscalité IS",
            2 => "Réorganisations, acquisitions et méthodologie",
            3 => "Contrôle et contentieux fiscal",
            4 => "Fiscalité patrimoniale",
            5 => "Fiscalité Immobilière",
            6 => "Introduction à la TVA et droits de douanes",
            7 => "Excel"];
echo '<select name="categorie" id="cat-select">';
foreach ($options as $optionNo => $optionText) {
    echo '<option value="' . $optionNo . '"';
    if ($optionNo == $rows['categorie']) {
        echo ' selected';
    }
    echo '>' . $optionText . '</option>';
}
echo '</select>';

?>

See: https://3v4l.org/8qdYJ

If your site has multiple selects this code could be turned into a function.

  • Related