注意:未定义索引:变量 [英] Notice: Undefined index: variable

查看:71
本文介绍了注意:未定义索引:变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建以下表单:

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 代码:

<?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>';
    }
  }
?>

但不断收到这些错误:

注意:未定义索引:名称在 C:\xampp\htdocs\bet4info\Index.php 上第 19 行

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

注意:未定义索引:电子邮件在 C:\xampp\htdocs\bet4info\Index.php在线 20

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

注意:未定义索引:C:\xampp\htdocs\bet4info\Index.php 中的消息在线 21

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

注意:未定义索引:human in C:\xampp\htdocs\bet4info\Index.php在线 25

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

注意:未定义索引:提交在 C:\xampp\htdocs\bet4info\Index.php在线 29

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

注意:未定义索引:提交在 C:\xampp\htdocs\bet4info\Index.php第 35 行

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

注意:未定义索引:提交在 C:\xampp\htdocs\bet4info\Index.php在线 38

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

为什么会这样?

推荐答案

代替

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

使用

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

这篇关于注意:未定义索引:变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆