Note: Undefined index: variable

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>

      

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 I keep getting these errors:

Note: undefined index: name in C: \ xampp \ htdocs \ bet4info \ Index.php on line 19

Note: undefined index: email at C: \ xampp \ htdocs \ bet4info \ Index.php on line 20

Note: undefined index: message to C: \ xampp \ htdocs \ bet4info \ Index.php on line 21

Note: undefined index: person in C: \ xampp \ htdocs \ bet4info \ Index.php on line 25

Note: undefined index: send to C: \ xampp \ htdocs \ bet4info \ Index.php on line 29

Note: undefined index: send to C: \ xampp \ htdocs \ bet4info \ Index.php on line 35

Note: undefined index: send to C: \ xampp \ htdocs \ bet4info \ Index.php on line 38

Why is this so?

+3


source to share


2 answers


Instead

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

      



using

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

      

+4


source


To do this, you first need to check using the function isset

:

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

      



As it is, the same for all variables.

0


source







All Articles