-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!

  • 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. – droopsnoot Jun 09 '20 at 08:31
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – CBroe Jun 09 '20 at 08:32

1 Answers1

1

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

if(isset($_POST['submit']){
   //your code
}
RainDev
  • 1,098
  • 8
  • 9
  • No, only part where you need access to POST variable – RainDev Jun 09 '20 at 08:34
  • 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; – mohammad erfan Jun 09 '20 at 08:39
  • I really don't know what the only error is: Notice: Undefined variable: errors in E:\Programing\Test\PHP\index.php on line 29 – mohammad erfan Jun 09 '20 at 11:53