-2

I have a php simple project. The project is to create an email submission page. I use phpstorm software.

        <?php
        //get user input
        $name = $_POST["name"];
        $email = $_POST["email"];
        $message = $_POST["message"];
        //error message
        $missingName = '<p><strong>Please enter your name</strong></p>';
        $missingEmail = '<p><strong>Please enter your email address</strong></p>';
        $invalidEmail = '<p><strong>Please enter a valid email address</strong></p>';
        $missingMessage = '<p><strong>Please enter a message</strong></p>';
        //if the user has submitted the form
        if ($_POST["submit"]){
            //check for errors
            if (!$name){
                $errors .= $missingName;
            }else{
                filter_var($name, FILTER_SANITIZE_STRING);
            }
            if (!$email){
                $errors .= $missingEmail;
            }else{
                filter_var($email, FILTER_SANITIZE_EMAIL);
                if (!filter_var($invalidEmail, FILTER_VALIDATE_EMAIL)){
                    $errors .= $invalidEmail;
                }
            }
            if (!$message){
                $errors .= $missingMessage;
            }else{
                $message = filter_var($message, FILTER_SANITIZE_STRING);
            }
            //if there are any errors
            if ($errors){
                $resultMessage = '<div class="alert alert-danger">' . $errors . '</div>';
            }

        }
        ?>
        <form action="index.php" method="post">
            <div class="form-group">
                <label for="name">Name:</label>
                <input type="text" name="name" placeholder="Name" id="name" class="form-control">
            </div>
            <div class="form-group">
                <label for="email">Email:</label>
                <input type="text" name="email" placeholder="Email" id="email" class="form-control">
            </div>
            <div class="form-group">
                <label for="message">Message:</label>
                <textarea name="message" id="message" class="form-control" rows="5"></textarea>
            </div>
            <input type="submit" name="submit" id="submit" class="btn btn-success btn-lg" value="Send Message">
        </form>

When I run the project, I encounter the errors you see in the image

error messages

Errors are from the following lines

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

And this line

if ($_POST["submit"])

please help!

2
  • You need to run your form-processing code only if the form has been submitted, you are running it regardless of that. Surround the form-processing code in an if ($_SERVER['REQUEST_METHOD'] == "POST") clause. Or put the processing code in a separate file and reference it in your form action parameter. Commented Jun 9, 2020 at 8:31
  • 1
    Does this answer your question? "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP Commented Jun 9, 2020 at 8:32

1 Answer 1

1

Before accessing POST variable you should check if its not empty:

if(isset($_POST['submit']){
   //your code
}
Sign up to request clarification or add additional context in comments.

3 Comments

No, only part where you need access to POST variable
The previous errors were fixed, but when I click on submit, it gives a new error: Notice: Undefined variable There is an error for this line => $errors .= $missingName;
I really don't know what the only error is: Notice: Undefined variable: errors in E:\Programing\Test\PHP\index.php on line 29

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.