首页 / PHP / php – 注意:未定义的索引:变量
php – 注意:未定义的索引:变量
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 注意:未定义的索引:变量,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2326字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试构建以下形式:
<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>';
}
}
?>
但不断得到这些错误:
Notice: Undefined index: name in C:\xampp\htdocs\bet4info\Index.php on
line 19Notice: Undefined index: email in C:\xampp\htdocs\bet4info\Index.php
on line 20Notice: Undefined index: message in C:\xampp\htdocs\bet4info\Index.php
on line 21Notice: Undefined index: human in C:\xampp\htdocs\bet4info\Index.php
on line 25Notice: Undefined index: submit in C:\xampp\htdocs\bet4info\Index.php
on line 29Notice: Undefined index: submit in C:\xampp\htdocs\bet4info\Index.php
on line 35Notice: 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') {
内容总结
以上是互联网集市为您收集整理的php – 注意:未定义的索引:变量全部内容,希望文章能够帮你解决php – 注意:未定义的索引:变量所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。