1 Reply Latest reply: Oct 4, 2014 9:10 PM by Ben Pleysier RSS

    Notice: Undefined Index Error

    Max Resnikoff Community Member

      Why do I get this error message when the page is loaded?

       

      Notice: Undefined index: name in C:\xampp\htdocs\dj-direct\test.php on line 6

       

      Notice: Undefined index: email in C:\xampp\htdocs\dj-direct\test.php on line 7


      Here is the page code:


      <?php

       

      $to = 'email@email.com';

      $subject = 'Contact Form Submission';

       

      $name = $_POST['name'];

      $email = $_POST['email'];

      $message = <<<EMAIL

      This is your name:

      $name

       

      This is your email:

      $email

      EMAIL;

       

      $header = '$email';

       

      if($_POST){

          mail($to, $subject, $message, $header);

      }

       

      ?>

       

       

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml">

      <head>

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

      <title>Test</title>

      </head>

       

      <body>

      <form method="post" action="?">

        <p>

          <label for="name">Name: </label>

          <input type="text" name="name" id="name" />

        </p>

        <p>

          <label for="email">Email: </label>

          <input type="text" name="email" id="email" />

        </p>

        <p>

          <input type="submit" name="submit" id="submit" value="Submit" />

        </p>

      </form>

       

      </body>

      </html>

        • 1. Re: Notice: Undefined Index Error
          Ben Pleysier CommunityMVP

          When you open the document that contains

          $name = $_POST['name'];

          $email = $_POST['email'];

          there will have been no posted data, hence the variables will not have been populated, i.e. neither the index or the value will have been set.

           

          You can overcome the error message you can use isset() as in

          if (isset($_POST["name"]) && !empty($_POST["name"])) {

              $name = $_POST['name'];   

          }else{ 

              echo "name is not set";

          }