lundi 23 mars 2020

PHP Form required input fields just required when displayed

i have a question about a PHP / AjAx Form im using at the moment. I changed it for my personal needs, but I stuck at some point.

First of all the form: https://bemayo-hosting.de/tanzzentrum-bamberg/

What exactly is my problem?

The customer can choose the number of persons at the beginning. If he choose "2 persons" another DIV shown up with 2 input fields.

Thats the code to make the div visible on click:

<script>

function personenCheck(that) {
    if (that.value == "2") {
        alert("Hinweis: Sie haben eine weitere Person hinzugefügt. Bitte beachten Sie, dass die Preise der einzelnen Kurse pro Person berechnet werden.");
        document.getElementById("weitere_person").style.display = "block";
    } else {
        document.getElementById("weitere_person").style.display = "none";
    }
}
</script>

So far so good.

Hier is the code for the extra fields popping up after selecting a 2nd person:

<div style="display:none;" id="weitere_person">
    <h5><strong>Angaben zur 2. Person:</strong></h5>

    <div class="form-group has-feedback">
        <input name="name2" id="name2" class="form-control" type="text"
                   data-error="Bitte Namen angeben">
        <div class="input-group-icon"><i class="fa fa-user"></i></div>
        <label class="form-label">Vor- & Nachname </label>
    </div><!-- end form-group -->

    <div class="form-group has-feedback">
        <input name="fbday2" id="fbday2" class="form-control" type="text
                   data-error="Bitte Geburtsdatum Persomn 2 angeben">
        <div class="input-group-icon"><i class="fa fa-calendar"></i></div>
        <label class="form-label">Geburtsdatum TT.MM.JJJJ</label>
    </div><!-- end form-group -->

</div>

So know my problem is: I didnt marked them fields as required fields, because if the customer just select one person and the div stays invisible the form tells the customer that he has to fill it out, but yeah.. he cant.

So i have to know how to avoid this in the PHP file. Here is an example how the firm checks the normal birthday field thats visbile for 1 person all the time (thats works fine):

if (empty($_POST["bday"])) {
    $errormsg .= "Bitte Geburtsdatum angeben ";
} else {
    $bday = filter_var($_POST['bday'], FILTER_SANITIZE_STRING);
}

And thats for example the check for brithdate of the 2nd person, so the not shown DIV:

if (($_POST["fbday2"])) {
    $fbday2 = filter_var($_POST['fbday2'], FILTER_SANITIZE_STRING);
};

I changed it and got rid of the if empty state, because it gave me errors the whole time, that customerhas to fill it out. So i changed it and deleted in the HTML the "required".

But now i need to set it up as an reuired input field, but i dont know how to change the if state to make it work even its not visible.

Sorry for the complicated explanation, but im quiet new to PHP.

Thanks.

Aucun commentaire:

Enregistrer un commentaire