On submit, echo 'selected' on the right option of a select tag if form return false

StackOverflow https://stackoverflow.com/questions/21119319

  •  28-09-2022
  •  | 
  •  

Вопрос

I got an issue right now, and I really don't understand why, lol. Let me explain it!

What I want to do : If form has been submitted and return false, take "correct" values within the form and place it into his right input value so that the customer won't fill it again. (In this situation, it's about a select tag with many options)

What it's doing : Well, when I click on submit, my option selected it's ALWAYS the value 3 (5 ans et plus) but when I try to echo out $_POST['depuis'], it is the right value that echos out.

Here's my partial script :

<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<select id="depuis" name="depuis">
                            <option value='0' <?php if(isset($_POST['depuis']) == '0'){ echo 'selected'; } else { echo ''; } ?> ></option>
                            <option value='1' <?php if(isset($_POST['depuis']) == '1'){ echo 'selected'; } else { echo ''; } ?> >2 ans et moins</option>
                            <option value='2' <?php if(isset($_POST['depuis']) == '2'){ echo 'selected'; } else { echo ''; } ?> >2 a 5 ans</option>
                            <option value='3' <?php if(isset($_POST['depuis']) == '3'){ echo 'selected'; } else { echo ''; } ?> >5 ans et plus</option>
                        </select>
                        <?= $_POST['depuis'] ?>
                        <input type="submit">

</form>
Это было полезно?

Решение

Replace

if(isset($_POST['depuis']) == '0')

with

if(isset($_POST['depuis']) &&  $_POST['depuis'] == '0')

and so on...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top