0

I'm learning coding and created a simple form where error messages are displayed just below each input field. However, when I check the form the success message appears at the same time as error messages instead of displaying when all the fields are correctly entered and form validated. Can you please help. Thank yo in advance. Here is my code.

</php>

$errorMessage = "";
$successMessage = "";
$emailError = "";
$emailconfirmError = "";
$nameError = "";
$messageError = "";
$servicesError = "";


$name = $email = $emailConfirm = $services = $message = "";

$email = isset($_POST['email']) ? $_POST['email'] : '';
$emailConfirm = isset($_POST['emailConfirm']) ? $_POST['emailConfirm'] : '';


if ($_POST) {

    if (!$_POST['email']) {

        $emailError .="The email is required";

    }

    if (!$_POST['emailConfirm']) {

        $emailconfirmError .="Please confirm your email <br>";

    }
    if ($_POST['emailConfirm'] && $email != $emailConfirm) {

       $emailconfirmError .="The email addresses do not match <br>";   
}

    if (!$_POST['name']) {

        $nameError .="The name field is required <br>";

    }

    if (!$_POST['services'])  {

       $servicesError .="Please select a service required <br>";

    }

    if (!$_POST['message']) {

        $messageError .="The message field is required <br>";

    }


    if ($_POST['email'] && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {

       $emailError .= "The email address is invalid.<br>";

    }

    if ($name = $email = $emailConfirm = $services = $message != "") {

        echo $emailError;
        echo $emailconfirmError;
        echo $name;
        echo $services;
        echo $message;

    }else {

        $emailTo = "kamala_guliyeva@hotmail.com";

        $services = $_POST['services'];

        $message = $_POST['message'];

        $headers = "From: ".$_POST['email'];



    if (mail($emailTo, $services, $message, $headers)) {

            $successMessage = '<div class="alert alert-success" role="alert">Thank you for your message. We\'ll get back to you ASAP!</div>';


        } else {

            $errorMessage = '<div class="alert alert-danger" role="alert"><p>Your message couldn\'t be sent - please try again</div>';

        }
    }
}        

and HTML

<div id="quote">
<div class="container">
        <h2 class="section-title">Request a Quote</h2>
        <hr align="left" width="8%" class="style-one">
        <br>
    <div><? echo $errorMessage.$successMessage; ?></div>
      <form id="quoteForm" method="post">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <input type="email" class="form-control" style="height:60px" id="email" name="email" placeholder="Your email">
                    <label class="error" id="emailError"><?php echo $emailError; ?></label>
                </div>

                <div class="form-group">
                    <input type="email" class="form-control" style="height:60px" id="emailConfirm" name="emailConfirm" placeholder="Re-type your email">
                    <label class="error" for="e-mailConfirm" id="emailconfirmError"><?php echo $emailconfirmError; ?></label>
                </div>

                 <div class="form-group">
                    <input type="name" class="form-control" id="name" style="height:60px" name="name" placeholder="Your Name">
                    <label class="error" for="name" id="nameError"><?php echo $nameError; ?></label>
                </div> 

                </div>   

            <div class="col-md-6">
                <div class="form-group">
                    <select class="form-control" id="services" name="services" style="height:60px">
                      <option value="">Select Services</option>
                      <option value="Installation">Installation</option>
                      <option value="Repair">Repair</option>
                      <option value="Service and Maintenance">Service and Maintenance</option>
                    </select>
                    <label class="error" for="services" id="servicesError"><?php echo $servicesError; ?></label>
                  </div>
              <div class="form-group">
                <textarea class="form-control" id="message" name="message" placeholder="Message" style="height: 163px;" cols="35"></textarea>
                <label class="error" for="message" id="messageError"><?php echo $messageError; ?></label>
              </div>  
             </div> 
             </div>

            <div class="form-row text-center">
                <div class="col-12">
                    <button type="submit" style="width:10rem" class="btn quoteButton pt-3 pb-3 text-align-center">Get a Quote</button>
                </div>
            </div>

    </form>

</div>
</div>
Qirel
  • 25,449
  • 7
  • 45
  • 62
Kama
  • 1
  • 1

1 Answers1

0

You've a few problems there...

So first thing is how you are doing your checks.

if(!$_POST) {

is not a valid way of checking that a post has occurred you need to do something like

if(isset($_POST) && !empty($_POST)) {

would be more appropriate as you are checking if the POST array is actually set and then that it is not empty the && operator is a short circuit operator so if either condition isn't met then the check will fail.

Similarly on your comparisons saying if(!$_POST['email']) { isn't valid because you're effectively asking "if the email part of the post array is not true" where as you need to be asking "if it's not blank and is a valid email address"

You need to be aware of the difference between = == and === operators. You can find some more information here: The 3 different equals

And also the filter_var function here: http://php.net/manual/en/function.filter-var.php

if($_POST['email']!=="" && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    //all good
} else {
   //set your error message
}

you can use regular expressions to validate 'name' see preg_match in the manual http://php.net/manual/en/function.preg-match.php

Aside from all of this if you want to have real time monitoring of the fields and the error messages etc you're going to have to look at incorporating Javascript and using AJAX to communicate with your script and then parse the response back into the appropriate div.

Have a look at Rasmus 30 second AJAX tutorial will give you a starting point for this http://rajshekhar.net/blog/archives/85-Rasmus-30-second-AJAX-Tutorial.html

However it is better practice to do both client and server side validation hope this helps even if it is not a complete answer per sé.

  • 1
    There's no need to check both `isset()` and `!empty()`. It's redundant. Further, everything coming from `$_POST` is a string, meaning that strict comparisons aren't necessary. In my opinion, its also better to check `empty()` rather than comparing to an empty string. Checking `!$_POST` is also a valid method to see if there is any elements in the POST-array (if its a good practice to do is an entirely different subject, but it's still *valid*. ;-) Otherwise, a good answer! – Qirel Nov 11 '18 at 12:48