0

I am trying to get a simple HTML form to send me an email when someone submits a response. I'm not very familiar with PHP, but I found some examples online and tried to implement it myself. So far, when the submit button is clicked, I am redirected to the "quote.php" page and it just shows a blank white screen, and I don't get an email. I would really appreciate any insight into what I'm doing wrong!

form.html

<form class="from-contact" action="quote.php" method="post">
                        <input name="name" type="text" class="form-control" placeholder="Name">
                        <input name="email" type="email" class="form-control" placeholder="Email">
                        <textarea name="message" class="form-control textarea" placeholder="Description"></textarea>
                        <button type="submit" class="btn btn-default button submit">
                            submit now<i class="fa fa-angle-double-right"></i>
                        </button>
                    </form>

quote.php

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

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

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

        // 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>';
    }
}
    }
?>
  • 1
    What if you trade `isset($_POST["submit"])` for `!empty($_POST)` – QuickDanger Sep 08 '17 at 17:27
  • Edit: This allowed the email to send, but I'm still being redirected to a blank page. – Jeremy Collins Sep 08 '17 at 17:43
  • Try reworking your logic such that things aren't as nested as they are. Ex: instead of "if (true) -> if (true)" use "if (false) die("error!")" to keep things flat and easy to follow. The snippet you've posted doesn't echo out `$result`. Any other issue is in code you haven't posted. – QuickDanger Sep 08 '17 at 18:01
  • Thanks guys, I was able to figure it out with your help! I just needed to rework the code and make sure to fix the $result variable so that it displays. – Jeremy Collins Sep 08 '17 at 18:33

0 Answers0