Frontend form active selected option?

Hello,

I displayed on the frontend side a select form field with options.
I would like what I have selected to remain in “selected”.

How can I indicate the “selected” field?
Is there an isSelected() or something like that? I already know isOpen() for pages. I would like something equivalent.

I tried:
<option value="<?= $value ?>" <?= (isset($value) && $value == $value) ? "selected":''; ?>><?= $option ?></option>

But it “selected” all the fields.

I then did:
<option value="<?= $value ?>" <?= (isset($_POST['sex']) && $_POST['sex'] == $value) ? "selected":''; ?>><?= $option ?></option>

But if we refresh the page the selection is no longer made. (the data is well preserved in the content).

Thanks for your help

Could you post your form and logic please?

Here is the complete form:


<?php if($error): ?>
<span class="p-alert red"><?= $message ?></span>
<?php endif; ?>
<?php if($success): ?>
<span class="p-alert green">Mise à jour avec succès !</span>
<?php endif; ?>

<form action="" method="post">

    <div class="form-floating mb-3">
        <input type="text" class="form-control" id="firstname" name="firstname" placeholder="Prénom" value="<?= $kirby->user()->firstname() ?>">
        <label for="Prénom">Prénom</label>
    </div>
    <div class="form-floating mb-3">
        <input type="text" class="form-control" id="namefamily" name="namefamily" placeholder="Nom" value="<?= $kirby->user()->namefamily()->upper() ?>">
        <label for="namefamily">Nom</label>
    </div>

    <?php
    //Liste les values
    $options = $kirby->user()->blueprint()->field('sex')['options'];
    var_dump($options);
    ?>
    <div class="form-floating mb-3">
        <select class="form-select" aria-label="Sexe" id="sex" name="sex">
        <!-- <option selected>Sexe</option> -->
        <option>Sexe</option>
        <?php foreach ($options as $value => $option): ?>
        <option value="<?= $value ?>" <?= (isset($_POST['sex']) && $_POST['sex'] == $value) ? "selected":''; ?>><?= $option ?></option>
        <?php endforeach ?>
        </select>
        <label for="sex">Sexe</label>
    </div>

    <div class="form-floating mb-3">
        <input type="text" class="form-control" id="birthday" name="birthday" placeholder="Date de naissance" value="<?= $kirby->user()->birthday() ?>">
        <label for="birthday">Date de naissance</label>
    </div>
        
    <input class="btn-solid-lg text-uppercase px-5" type="submit" name="update" value="Modifier mes informations">
</form>

here is the part of the blueprint:

          sex:
            label: Sexe
            type: select
            default: sex_default
            options:
              sex_man: Homme
              sex_woman: Femme
              sex_default: Non défini

Hm, that should actually work. But I probably wouldn’t use $_POST but the variable you use in your controller to store the data.

But if it is all about showing the currently stored value, then you should use that if there is not post data.

<option value="<?= $value ?>" <?= ((isset($_POST['sex']) && $_POST['sex'] === $value) || $kirby->user()->sex()->value() === $value ) ? "selected":''; ?>><?= $option ?></option>        

I deleted the $ _POST, the correct answer is:

<?= ($kirby->user()->sex()->value() == $value) ? "selected":''; ?>

You are wonderful, I’ve been walking around for 2 hours. I should ask you faster.

Thank you very much.

I will tackle the next field to display the dates.
I do not yet know how to make the date well written on the frontend side.

Well, this works as long as there is a value already stored and the form has no errors. However, if the user wants to change the value and reloads the page or the form has an error, it will not work as expected and the stored value will be shown as selected again. Therefore the OR condition in my suggestion above.

Ok, thanks you !