Friday, June 21, 2019

php - Notice: Undefined index: variable



I am trying to build the following form:

























The PHP code:




  $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 '

Your message has been sent!

';
} else {
echo '

Something went wrong, go back and try again!

';
}
} else if ($_POST['submit'] && $human != '4') {
echo '

You answered the anti-spam question incorrectly!

';
}


if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '

Your message has been sent!

';
} else {
echo '

Something went wrong, go back and try again!

';
}
} else if ($_POST['submit'] && $human != '4') {

echo '

You answered the anti-spam question incorrectly!

';
}
} else {
echo '

You need to fill in all required fields!!

';
}
}
?>


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?


Answer



Instead of



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



use



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

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...