Search code examples
phphtmlemailphpmailercontact-form

How Can I Echo This PHP 'Field Required' Message On My HTML Page?


I'm building a PHP form. This script cycles through the form's fields and tells me if they're empty or not. When I submit the form, it redirects me from index.html to contact.php and displays a blank page. I would like to display $errmsg above the form in index.html. What am I doing wrong? Here's where I'm pulling information from: http://www.w3schools.com/php/php_form_required.asp

PHP:

<?php
$fields = array('name', 'email', 'telephone');
$error = false;
foreach($fields AS $fieldname) {
        if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
                $errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
                $error = true;
    }
}

HTML:

<form class="form-horizontal" role="form" method="post" action="contact.php">
    <div class="form-group">
            <span class="error">* <?php echo $errmsg;?></span>

Here is my entire contact.php in case the issue is further down.

<?php
$fields = array('name', 'email', 'telephone');
$error = false;
foreach($fields AS $fieldname) {
        if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
                $errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
                $error = true;
        }
}

if(!$error) {
        if (isset($_POST["submit"])) {
                $name = $_POST['name'];
                $email = $_POST['email'];
                $telephone = $_POST['telephone'];
                $message = $_POST['message'];
                $headers = "From: [email protected]\r\n";
                $to = '[email protected]';
                $subject = 'New Lead';
                $body = "From: $name\n E-Mail: $email\n Phone: $telephone\n Message: $message";
        mail('[email protected]', $subject, $body, $headers);
        header("Location: http://www.example.com/messagesent.html");
        die();
        }
}
?>

edit: Entire HTML Form. It's nested in Bootstrap sorry for spaghetti

<!-- Contact Form -->
<form class="form-horizontal" role="form" method="post" action="contact.php">
        <div class="form-group">
                <span class="error">* <?php echo $errmsg;?></span>
                <label for="name" class="col-sm-2 control-label">Name</label>
                <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name">
                </div>
        </div>
        <div class="form-group">
                <label for="email" class="col-sm-2 control-label">Email</label>
                <div class="col-sm-10">
                        <input type="email" class="form-control" id="email" name="email" placeholder="[email protected]">
                </div>
        </div>
                <div class="form-group">
                <label for="telephone" class="col-sm-2 control-label">Phone</label>
                <div class="col-sm-10">
                        <input type="text" class="form-control" id="telephone" name="telephone" placeholder="250-555-555">
        </div>
        </div>
        <div class="form-group">
            <label for="message" class="col-sm-2 control-label">Message</label>
            <div class="col-sm-10">
                    <textarea class="form-control" rows="4" name="message"></textarea>
            </div>
    </div>
    <div class="form-group">
            <div class="col-sm-10 col-sm-offset-2">
                    <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
            </div>
        </div>
        </div>
</form>
<!-- End Contact Form -->

Solution

  • **Use Ajax Method**    
    
    **index.html** 
    
        <!DOCTYPE html>
        <html lang="en">
        <head>
        <meta charset="utf-8">
        <title>using Ajax in jQuery</title>
        <style type="text/css">
            label{
                display: block;
                margin-bottom: 10px;
            }
        </style>
        <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function(){
            $("form").submit(function(event){
                // Stop form from submitting normally
                event.preventDefault();
    
                /* Serialize the submitted form control values to be sent to the web server with the request */
                var formValues = $(this).serialize();
    
                // Send the form data using post
                $.post("contact.php", formValues, function(data){
                    // Display the returned data in browser
                    $("#result").html(data);
                });
            });
        });
        </script>
        </head>
        <body>
            <form>
                <label>Name: <input type="text" name="name"></label> 
                <input type="submit" value="Send">
            </form>
            <div id="result"></div>
        </body>
        </html>             
    
    
    
    contact.php 
    
    
    <?php
    $fields = array('name');
    $error = false;
    foreach($fields AS $fieldname) {
            if(!isset($_POST['name']) || empty($_POST['name'])) {
                    $errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
                    $error = true;
            }
    }
    
    if($error==true){
        echo $errmsg;
        die();
    }
    else{
            if (isset($_POST["submit"])) {
                    $name = $_POST['name']; 
                    $headers = "From: [email protected]\r\n";
                    $to = '[email protected]';
                    $subject = 'New Lead';
                    $body = "From: $name\n E-Mail: $email\n Phone: $telephone\n Message: $message";
            mail('[email protected]', $subject, $body, $headers);
            header("Location: http://www.example.com/messagesent.html");
            die();
            }
    }
    ?>