-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 ?
Zoy, your code is showing a syntax error but I couldn’t identify it.
– Paulo Fernando Cé
I found it. Thank you very much.
– Paulo Fernando Cé
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.
– Paulo Fernando Cé