Php 注意:未定义索引:变量
我正在尝试构建以下表单:Php 注意:未定义索引:变量,php,mysql,xampp,Php,Mysql,Xampp,我正在尝试构建以下表单: <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
<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>
名称
电子邮件
我需要一些关于:
*什么是2+2?(反垃圾邮件)
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行
注意:未定义索引:C:\xampp\htdocs\bet4info\index.php中的电子邮件
第20行
注意:未定义的索引:C:\xampp\htdocs\bet4info\index.php中的消息
在线21
注意:C:\xampp\htdocs\bet4info\index.php中未定义的索引:human
第25行
注意:未定义的索引:在C:\xampp\htdocs\bet4info\index.php中提交
第29行
注意:未定义的索引:在C:\xampp\htdocs\bet4info\index.php中提交
第35行
注意:未定义的索引:在C:\xampp\htdocs\bet4info\index.php中提交
第38行
为什么会这样?而不是
$name = $_POST['name'];
if ($_POST['submit'] && $human == '4') {
使用
为此,首先必须使用
isset
功能进行检查:
if(isset($_POST['name']) && isset($_POST['email']) &&....)
{
// If isset then assign data to variable $name = $_POST['name'],...
}
else
{
// Redirect to the error page
}
与此类似,所有变量也是如此。因为您从$\u POST设置了示例名称,并且在check is values之前不使用isset函数,这对我有效,但您需要在
$\u POST
值上应用isset()
。@Anantkumarsing错了。那是因为什么?您需要在您认为无法初始化的每个变量上使用isset,例如php建议,不一定在$\u POST上使用,但仅在$name上使用isset(在OP案例中),如果您的给定代码位于单个文件中,您肯定会遇到这些错误。我说$\u POST值意味着他需要检查的每个值。请仔细阅读我的评论。
if(isset($_POST['name']) && isset($_POST['email']) &&....)
{
// If isset then assign data to variable $name = $_POST['name'],...
}
else
{
// Redirect to the error page
}