3

I am trying to build the following form:

<form method="post" action="Index.php">
  <label>Name </label>
  <input name="name" placeholder="Type Here"><br />
  </br>
  <label>Email </label>
  <input name="email" placeholder="Type Here">
  <br /></br>
  <label style="display:block;">I need some information regarding:</label>
  <textarea name="message" placeholder="Type Here"></textarea>
  <br />
  <label>*What is 2+2? (Anti-spam)</label>
  <input name="human" placeholder="Type Here">
  <br />
  <input id="submit" name="submit" type="submit" value="Submit" style="height:30px;">
</form>

The PHP code:

<?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $from = 'From: TangledDemo';
  $to = 'hello@emailaddress.com';
  $subject = 'Hello';
  $human = $_POST['human'];

  $body = "From: $name\n E-Mail: $email\n Message:\n $message";

  if ($_POST['submit'] && $human == '4') {
    if (mail ($to, $subject, $body, $from)) {
      echo '<p>Your message has been sent!</p>';
    } else {
      echo '<p>Something went wrong, go back and try again!</p>';
    }
  } else if ($_POST['submit'] && $human != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
  }

  if ($_POST['submit']) {
    if ($name != '' && $email != '') {
      if ($human == '4') {
        if (mail ($to, $subject, $body, $from)) {
          echo '<p>Your message has been sent!</p>';
        } else {
          echo '<p>Something went wrong, go back and try again!</p>';
        }
      } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
      }
    } else {
      echo '<p>You need to fill in all required fields!!</p>';
    }
  }
?>

But constantly getting these errors:

Notice: Undefined index: name in C:\xampp\htdocs\bet4info\Index.php on line 19

Notice: Undefined index: email in C:\xampp\htdocs\bet4info\Index.php on line 20

Notice: Undefined index: message in C:\xampp\htdocs\bet4info\Index.php on line 21

Notice: Undefined index: human in C:\xampp\htdocs\bet4info\Index.php on line 25

Notice: Undefined index: submit in C:\xampp\htdocs\bet4info\Index.php on line 29

Notice: Undefined index: submit in C:\xampp\htdocs\bet4info\Index.php on line 35

Notice: Undefined index: submit in C:\xampp\htdocs\bet4info\Index.php on line 38

Why is it so?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3409468
  • 39
  • 1
  • 1
  • 7
  • Because you set as example name from a $_POST and do not use the isset function before check is value – Goikiu Jun 11 '15 at 08:34
  • This is working for me, but you need to apply `isset()` on `$_POST` values. – Alive to die - Anant Jun 11 '15 at 08:35
  • @anantkumarsingh Wrong. And that because? You need to use isset on each var you think can be not initilized, such as php suggest, not necessarily on $_POST but as example only on $name (in OP case) – Goikiu Jun 11 '15 at 08:37
  • If your given code is in a single file, you will definitely get those errors. – Logan Wayne Jun 11 '15 at 08:40
  • i said $_POST values, means each value he need to check. just read my comment completly. – Alive to die - Anant Jun 11 '15 at 08:40
  • @anantkumarsingh to explain a little more, is it correct to check $_post value (if exist) before setting variables, but you need to use isset on each var (generic) that can be not existent before using it, otherwise you'll get an error and not a notice. – Goikiu Jun 11 '15 at 08:41
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Logan Wayne Jun 11 '15 at 08:46

2 Answers2

5

Instead of

$name = $_POST['name'];
if ($_POST['submit'] && $human == '4') {   

use

$name = isset($_POST['name']) ? $_POST['name'] : '';
if (isset($_POST['submit']) && $human == '4') {   
Alexey Rytikov
  • 558
  • 6
  • 12
0

Into this, first you have to check using the isset function:

if(isset($_POST['name']) && isset($_POST['email']) &&....)
{
    // If isset then assign data to variable $name = $_POST['name'],...
}
else
{
    // Redirect to the error page
}

As like this, the same for all the variables.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35