Search code examples
phphtmlformsrequiredfieldvalidator

PHP error on all fields required - but all fields are inserted


I'm making a signup form (signup.html) with a php handler (signup-form-handler.php). But it seems like I'm doing something wrong.

This is my code:

$errors = '';
$myemail = 'example@domain.com';//<-----Put Your email address here.
if(empty($_POST['FirstName'])  || 
   empty($_POST['LastName'])  || 
   empty($_POST['City'])  || 
   empty($_POST['Country']))
{
    $errors .= "\n Error: all fields are required";
}

$FirstName = $_POST['FirstName']; 
$LastName = $_POST['LastName'];
$City = $_POST['City'];
$Country = $_POST['Country'];
$Int1 = $_POST['Int1'];       // <---- Here's what seems to be the issue
$Int2 = $_POST['Int2'];       // <---- Here's what seems to be the issue 

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Udfyldelse af test på Gamification: $FirstName";
    $email_body = "Du har fået en ny besvarelse. ".
    "Her er besvarelserne:\n Fornavn: $FirstName \n Efternavn: $LastName \n By: $City \n Land $Country \n".
    "De fire personlige interesser: $Int1 og $Int2"; 

    $headers = "From: $myemail\n"; 

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
} 

Here's the relevant html code:

<form id="contact_form" name="contact_form" action="signup-form-handler.php">


    <!-- Personlige oplysninger -->
    <div class="next">

        <h3>Personlige oplysninger<a class="info" href="#" title="Testing"><img style="height: 15px; width: 15px; margin-left: 5px;" alt="Time" src="img/signup_form/info_simple.png"></a></h3>


        <b>Mit navn er</b>
        <input class="user_field" placeholder="fornavn" id="FirstName" name="FirstName" type="text" maxlength="60" />
        <input class="user_field" placeholder="efternavn" id="LastName" name="LastName" type="text" maxlength="60" />
        </br>


        <b>og bor i</b>
        <input class="user_field" placeholder="indsæt by" id="City" name="City" type="text" maxlength="60" />
        <b>,</b>
        <input class="user_field" placeholder="indsæt land" id="Country" name="Country" type="text" maxlength="60" />
        </br>

        </br>
        <b>Beskriv dig selv med 4 interesser</b>
        </br>
        <input id="Int1" name="Int1" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 1" />
        <input id="Int2" name="Int2" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 2" />
        <input class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 3" />
        <input class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 4" />

    </div>

As you can see from my code example, I'm trying to get input from my an html form. I've commented in to the code, where the problem occurs. If I retrieve input from Int1 and Int2, I get the answer, that all required fields need to be filled in, but I don't want to require Int1 and Int2. Also I didn't add Int1 and Int2 in the beginning where I check if the other variables are empty. If I remove Int1 and Int2 it all works fine.

When I send the form after inserting all the inputs, I get the following message: "Error: all fields are required"

What am I doing wrong?

Hope I gave enough info to find the issue, thanks!


Solution

  • The default method is GET. Set your form method to POST:

    <form id="contact_form" name="contact_form" action="signup-form-handler.php" method="POST">
    

    So basically you're not passing the values to the script. If you wanted to use the GET method, switch all instances of $_POST to $_GET and you can leave your form as is.