1

I've made a PHP form to submit to self with error validation, but the form is not submitting. The idea is, when the user clicks on the submit button and hasn't filled in all required fields or email address they entered is flawed, then errors occur by adding an error class that's sorted by CSS. The CSS is fine, but the form is not submitting. I'd appreciate the help.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Email</title>
    </head>
    <body>
    <?php
        $error = '';
        $to = "[email protected]";

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (empty($_POST["name"]) || empty($_POST["email"]) || empty($_POST["message"])) {
                $error = 'class="error" ';
            } else {
                $name = stripslashes(trim($_POST["name"]));
                $email = stripslashes(trim($_POST["email"]));
                $message = stripslashes(trim($_POST["message"]));
                $pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';

                if (preg_match($pattern, $name) || preg_match($pattern, $email)) {
                    $error = 'class="error" ';
                }

                $emailIsValid = filter_var($email, FILTER_VALIDATE_EMAIL);

                if ($name && $email && $emailIsValid && $message) {
                    $subject = "From $name";
                    $body = "Name: $name <br /> Email: $email <br /> Message: $message";

                    $headers = "Reply-To: $email";

                    $success = mail($to, $subject, $body, $headers);
                    if ($success) {
                        header("Location: /email/sent/");
                    } else {
                        header("Location: /error/");
                    }
                }
            }
        }
        ?>

        <form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
            <input <?php echo $error; ?>type="text" name="name" placeholder="Full Name" spellcheck="false">
            <input <?php echo $error; ?>type="text" email="email" placeholder="Email Address" spellcheck="false">
            <textarea <?php echo $error; ?>type="text" message="message" placeholder="Message" rows="6" spellcheck="false"></textarea>
            <button type="submit" name="submitted">submit</button>
        </form>
    </body>
</html>
2
  • You have an error here: name="email" ..... <input <?php echo $error; ?>type="text" email="email" Commented Jan 6, 2018 at 6:01
  • You have similar error here: <textarea <?php echo $error; ?>type="text" MESSAGE="message" should be name="message" Commented Jan 6, 2018 at 6:02

1 Answer 1

1

NOTE : You have typo mistakes in your form tag.you used double quote inside double quote.

Insted of using this

<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
and
if ($_SERVER["REQUEST_METHOD"] == "POST") {

Use

<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
and
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submitbutton'])) {
//SO IT WILL PERFORM ONLY WHEN SUBMIT BUTTON WAS PRESSED

For More You can Learn it here

Or Also Live Demo is available

Sign up to request clarification or add additional context in comments.

1 Comment

there is no issue with the double quotes inside the double quotes. the php variable will be parsed without a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.