Select option selected

Asked

Viewed 2,232 times

-1

I need to leave the select option selected according to the field saved in mysql. The form is to edit so it displays the fields available with foreach.

Ex:

 <select name="segurado"  >
    <option value=""></option>
    <?php foreach ($result as $rs) { ?>
        <option value="<?php echo $rs["nome"]; ?>"><?php echo $rs["nome"]; ?></option>
    <?php } ?>
</select> 

This way select shows the table fields handled but I want to leave the record field selected for selected edition.


Maybe I didn’t get it right: I recover the field saved in mysql and I can put it in the select label but this is not the solution. But this shows that the variable to compare is correct, or whatever was recorded in the table when registering. As I want to edit the record, I need this recorded and recovered field to be set as Selected in options, which in turn is being fed by foreach and creating the elements in the same way as in the register. Below is an example of how I fill out the option:

                            $sql = "SELECT * FROM seguradoras ORDER BY nome";
                            try {
                                $stmt = $DB->prepare($sql);
                                $stmt->execute();
                                $results = $stmt->fetchAll();
                            } catch (Exception $ex) {
                                echo($ex->getMessage());
                            }

With it filled in, I want it to show the variable that comes from the bank in the case $residential->insurance

Improved galley ?

4 answers

0

Well, by changing the code and replacing the variables I finally got the result I wanted:

<?php foreach ($results as $rs) { ?>
    <option value="<?php echo $rs["nome"]; ?>" <?php if ($residencial->seguradora == $rs["nome"] ) { echo "selected";}  ?>  ><?php echo $rs["nome"]; ?></option>
<?php } ?>

Thank you all very much and a strong hug !

0

When meeting the condition you want to select put the <option selected>

<select name="segurado"  >
    <option value=""></option>
    <?php foreach ($result as $rs) { ?>
        <option value="<?php echo $rs["nome"]; ?>" <?php if($rs["minha_opcao_selecionada"] === true ){ echo "selected"}  ?>  ><?php echo $rs["nome"]; ?></option>
    <?php } ?>
</select>
  • Zoy, your code is showing a syntax error but I couldn’t identify it.

  • I found it. Thank you very much.

  • It looks like this:<option value="<? php echo $rs["name"]; ? >" <?php if ($residential->insured == $rs["name"] ) { echo "Selected";} ? > ><? php echo $rs["name"]; ? ></option> Missing semicolon after echo "Selected" Thanks Zoy, thank you very much.

0

Assuming you fill your form with entries in the table you could have a specific field for this type select with on or off or by checking the same example item:

<?php
    // sua lógica de acesso ao banco...
?>
<select name="segurado">
    <option value=""></option>
    <?php 
        foreach ($result as $rs) {
            // caso use um campo especifico
            if ( $rs['select'] === 'on' ) {
                echo '<option value="' . $rs['nome'] . '" selected="true">' . $rs['nome'] .'</option>';
            } else {
                echo '<option value="' . $rs['nome'] . '">' . $rs['nome'] .'</option>';
            }
            // ou pelo item mesmo
            if ( $rs['nome_do_item'] ) {
                echo '<option value="' . $rs['nome'] . '" selected="true">' . $rs['nome'] .'</option>';
            } else {
                echo '<option value="' . $rs['nome'] . '">' . $rs['nome'] .'</option>';
            }
        }
    ?>
</select> 
  • Lauro, I’ll try a little bit more before I choose to create the field with on off

0

If you want a compact option here’s a:

<select name="segurado">
    <option value=""></option>
    <?php foreach ($result as $rs): ?>
        <option value="<?=$rs['nome']?>" <?= ($rs['select'] == 'on') ?  'selected' : '' ?> ><?= $rs['nome'] ?></option>
    <?php endforeach; ?>
</select> 
  • 13dev, your code is selecting the first record of the table and duplicating all.

  • edited now tries so

Browser other questions tagged

You are not signed in. Login or sign up in order to post.