kim
kim

Reputation: 83

Make form fields required

I've tried to work with isset() to add required fields to my form as you can see below, but is this sufficient?
It only works for one field: ik_wens, because it's a checkbox. The form ignores the uncompleted textfields and just sends the e-mail.
What am I doing wrong?

<?php
    if (    !isset($_POST['naam']) ||
            !isset($_POST['adres']) ||
            !isset($_POST['tel']) ||
            !isset($_POST['datum_gourmet_fondue']) || 
            !isset($_POST['aantal_personen'])|| 
            !isset($_POST['ik_wens'])|| 
            !isset($_POST['graag'])
        ) {  
         echo 'U heeft niet alle velden ingevuld!';  
         exit;      
     } 

     $to = '[email protected]'; 
     $onderwerp = " Gourmet/ fonduelijst "; ;

     $naam = htmlspecialchars($_POST['naam']); 
     $adres = htmlspecialchars($_POST['adres']);
     $tel = htmlspecialchars($_POST['tel']);  
     $datum_gourmet_fondue = htmlspecialchars($_POST['datum_gourmet_fondue']);  
     $aantal_personen = htmlspecialchars($_POST['aantal_personen']);  
     $wish = $_POST["ik_wens"];
     $graag = htmlspecialchars($_POST['graag']); 

     $details = "
       Onderwerp: $onderwerp\n\n\n
       Naam: $naam\n\n
       Adres: $adres\n\n
       Tel.: $tel \n\n
       Datum gourmet/ fondue: $datum_gourmet_fondue \n\n
       Aantal personen: $aantal_personen \n\n
       Ik wens:  $wish \n\n
       Graag: $graag 
      ";

     // Send the message
     $ok = mail($to, $onderwerp, $details);
     if ($ok) {
         echo "<p>E-mail is verzonden</p>";
     } else {
         echo "<p>E-Mail is niet verzonden, probeer opnieuw!</p>";
     }
?>

Upvotes: 0

Views: 479

Answers (1)

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29975

You're using isset, which returns true for all elements that were present in the form. You want empty instead.

http://php.net/empty

Upvotes: 1

Related Questions