0

I have a contact form in which I am using PHP to complete the server side of things.

The issue I am encountering is that the form is submitting when the submit button is clicked but it is not displaying the form, the error message(s), or the success message.

When I reload the page, the form and the appropriate messages(s) appear.

I cannot see any clear reason as to why this would be so any help would great!

<?php

// $error and $success variable set to blank 
$error = ""; 
$successMessage = "";

// check to see if any $_POST variables have been entered
if ($_POST) {

    // if $_POST variable ["email"] has no value
    if (!$_POST["email"]) {
        // $error variable to add comment advising that data is missing
        $error .= "An email address is required!!!!!!!!<br>";
    }

    if (!$_POST["subject"]) {
        $error .= "The subject field is required!!!!<br>";
    }

    if (!$_POST["content"]) {
        $error .= "The content field is required!!!!!<br>";
    }

    // IF $error variable is no longer empty 
    if ($error != "") {
        // $error variable to genereate html + $error 
        $error = '<div><p><strong>Whoops! There were error(s) in the form:</strong></p>' . $error . '</div>';
    } else {    
        $emailTo = "email@example.com";
        $subject = $_POST["subject"];
        $content = $_POST["content"]; 
        $headers = "From: ".$_POST["email"];
        // IF all email variables have had values assigned then $successMessage to display html sucess message
        if (mail($emailTo, $subject, $content, $headers)) {
        $successMessage = '<div>Thank you for your enquiry, we will get back to you shortly!</div>';
        // ELSE email variable(s) are missing a value - $error variable to display html error message
        } else {
        $error = '<div>Unfortuantely your enquiry could not be sent, please try again later</div>';
        }

    }

}

?>

<html>

<div id="contactOuter">
    <div id="contactInner">
            <a href="javascript:void(0);"><span id="close">&times;</span></a>
            <h1>Get in touch</h1>
            <div><? echo $error.$successMessage;?></div>
                <form method="post">
                    <label for="email">Email address:</label><br>
                    <input name="email" type="email"  placeholder="Enter email" id="email">
                    <label for="subject">Subject:</label>
                    <input name="subject" type="text" id="subject">
                    <label for="content">What would you like to ask us?</label><br>
                    <textarea name="content" rows="7" id="content"></textarea>
                    <button type="submit" id="submit">Submit</button>
                </form>
    </div>
</div>

</html>
adprocas
  • 1,863
  • 1
  • 14
  • 31
Coder
  • 809
  • 1
  • 10
  • 23
  • You are outputting the error messages BEFORE the `` tag. Look at Page Source they will be there but the browser does not know what to do with them as they appear before the `` tag – RiggsFolly Apr 24 '18 at 16:37
  • Come to that you dont seem to have a `` tag either. HTML works best if you follow the basic rules – RiggsFolly Apr 24 '18 at 16:38
  • Any particular reason you don't have an ACTION specified for your form element? – Mario Lurig Apr 24 '18 at 16:39
  • @RiggsFolly, the errors are being `echo`ed after the opening `` tag. I agree with the `` comment. And, we should be using ` – adprocas Apr 24 '18 at 16:42
  • Possible duplicate of [PHP's white screen of death](https://stackoverflow.com/questions/1475297/phps-white-screen-of-death) – adprocas Apr 25 '18 at 14:55

1 Answers1

0

EDIT: Given the new information that the form disappears as well when you click on submit, you're likely getting a PHP error. Please read about PHP's white screen of death and enable error messaging so you can troubleshoot and see errors that you might have. I might also suggest looking into Xdebug or some other tool like that so you can step through your code to see exactly what is going on.

There are a few issues. First, always use <?php. That's from personal experience. Some servers are set up to not recognize <? believe it or not.

Second, based on the fact that the variables have no value when the form is printed to the screen, I don't think you're getting into your condition block. Try using if (isset($_POST["submit"])) { instead, and do <button type="submit" id="submit" name="submit">Submit</button> - Note: this may not be the case given the new information that the form isn't even showing up on form submit, but is still good practice.

As mentioned in the comments, include the <body> tag. Also, make sure you include other recommended and required markup (<!DOCTYPE html> and <html lang="en">, for example).

It's also recommended to use an action="file.php" attribute on the form.

<?php

// $error and $success variable set to blank 
$error = ""; 
$successMessage = "";

// check to see if any $_POST variables have been entered
if (isset($_POST["submit"])) {

    // if $_POST variable ["email"] has no value
    if (!$_POST["email"]) {
        // $error variable to add comment advising that data is missing
        $error .= "An email address is required!!!!!!!!<br>";
    }

    if (!$_POST["subject"]) {
        $error .= "The subject field is required!!!!<br>";
    }

    if (!$_POST["content"]) {
        $error .= "The content field is required!!!!!<br>";
    }

    // IF $error variable is no longer empty 
    if ($error != "") {
        // $error variable to genereate html + $error 
        $error = '<div><p><strong>Whoops! There were error(s) in the form:</strong></p>' . $error . '</div>';
    } else {    
        $emailTo = "email@example.com";
        $subject = $_POST["subject"];
        $content = $_POST["content"]; 
        $headers = "From: ".$_POST["email"];
        // IF all email variables have had values assigned then $successMessage to display html sucess message
        if (mail($emailTo, $subject, $content, $headers)) {
        $successMessage = '<div>Thank you for your enquiry, we will get back to you shortly!</div>';
        // ELSE email variable(s) are missing a value - $error variable to display html error message
        } else {
        $error = '<div>Unfortuantely your enquiry could not be sent, please try again later</div>';
        }

    }

}

?>

<html>
</body>

<div id="contactOuter">
    <div id="contactInner">
            <a href="javascript:void(0);"><span id="close">&times;</span></a>
            <h1>Get in touch</h1>
            <div><?php echo $error.$successMessage;?></div>
                <form method="post">
                    <label for="email">Email address:</label><br>
                    <input name="email" type="email"  placeholder="Enter email" id="email">
                    <label for="subject">Subject:</label>
                    <input name="subject" type="text" id="subject">
                    <label for="content">What would you like to ask us?</label><br>
                    <textarea name="content" rows="7" id="content"></textarea>
                    <button type="submit" id="submit" name="submit">Submit</button>
                </form>
    </div>
</div>

</body>    
</html>
adprocas
  • 1,863
  • 1
  • 14
  • 31
  • Does not seem to be working unfortunately and am confident that it is getting into the condition block. It is still giving me the same issue in which the form disappears on click of the submit button and only shows the error message(s) when I click to open the form again :( – Coder Apr 24 '18 at 17:01
  • The form disappears? You must have errors turned off and you're getting a PHP error. https://stackoverflow.com/questions/1475297/phps-white-screen-of-death – adprocas Apr 25 '18 at 13:05