1

I have a basic, 4-field PHP form for my website. I have it set up to email myself from the form the Name, Email, Phone and Message. This part works. What doesn't work is that when people don't fill in anything, there is no error message. Related, there is no success message when the submit button successfully sends a message to my email. Please advise and thank you in advance!

Here's my form and PHP

Index.php

<form class="form-horizontal" role="form" method="post" action="processes.php">
<div class="form-group">
    <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 &amp; Last Name" value="">
    </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="Shoot me an email!" value="">
        <?php echo "<p class='text-danger'>$errEmail</p>";?>
    </div>
</div>
<div class="form-group">
    <label for="phone" class="col-sm-2 control-label">Phone</label>
    <div class="col-sm-10">
        <input type="phone" class="form-control" id="phone" name="phone" placeholder="Let's talk!" value="">
    </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>
        <?php echo "<p class='text-danger'>$errMessage</p>";?>
    </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-default">
    </div>
</div>
<div class="form-group">
    <div class="col-sm-10 col-sm-offset-2">
        <! Will be used to display an alert to the user>
    </div>
</div>
</form>

From processes.php

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $from = 'Contact Form'; 
        $to = 'mybusiness@gmail.com'; 
        $subject = 'Contact Form';

        $body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";


        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
brando
  • 161
  • 1
  • 2
  • 13
  • 1
    I suppose it would make sense to echo the errors to the page? I see no code that prints $result to the page. – SirUncleCid May 29 '16 at 23:56
  • Ty for teh quick replay. I thought lines like ``$errEmail";?>`` were doing just that? Is my code wrong? – brando May 29 '16 at 23:58
  • `$errName` wasn't set previously. When you ask a question about an error, **ALWAYS** post the error log. To enable error reporting to your php code, append `error_reporting(E_ALL); ini_set('display_errors', '1');` at the top of your script, what does it return ? Also, can you believe php has a manual and `mail()`is there ?! http://php.net/manual/en/function.mail.php – Pedro Lobito May 29 '16 at 23:59
  • Possible duplicate of [How to send an email using PHP?](http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php) – Pedro Lobito May 30 '16 at 00:01
  • put the echo in span element..dont echo the element together the message – Mark Gerryl Mirandilla May 30 '16 at 00:30

1 Answers1

2

Attempt this, it has always worked for me! :)

First create an error output function:

function error_output($errors){
    return '<ol><li>' . implode('</li><li>', $errors) . '</li></ol>';
}

Then, inside of your code use errors[] = to set errors.

Something like this:

if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $errors[] = 'Please enter a valid email address';
}

    //Check if message has been entered
if (!$_POST['message']) {
    $errors[] = 'Please enter your message';
}

If the errors variable is not empty, output them to the page:

if(!empty($errors)){
    echo error_output($errors);
} else {
    //execute code
}

It should make your code much cleaner and easier to output errors!

Hope this helps!

GROVER.
  • 4,071
  • 2
  • 19
  • 66