PHP未定义的HTML表单变量

时间:2018-01-13 01:34:42

标签: php html variables contact-form isset

我正在尝试运行联系表单PHP脚本和HTML表单,当我运行它时,我收到以下错误。

  

注意:未定义的变量:第17行的/storage/ssd4/333/4197333/public_html/heros51/confirm.php中的名称

     

注意:未定义的变量:p在第18行的/storage/ssd4/333/4197333/public_html/heros51/confirm.php中

     

注意:未定义的变量:第18行的/storage/ssd4/333/4197333/public_html/heros51/confirm.php中的消息

     

注意:未定义的变量:第21行的/storage/ssd4/333/4197333/public_html/heros51/confirm.php中的visitor_email

<form action="confirm.php" method="POST" >
        <label>Name >></label><input id="name" type="text" /><br />
        <label>Email >></label><input id="email" type="text" /><br />
        <label>PRI >></label><input id="p" type="text" /><br />
        <label>Message >></label><textarea id="message" rows="1"></textarea><br /><br /><br />
        <input type="submit" value="submit" />
        <a class="button" alt="" href="index.html">Cancel</a>
    </form>

链接的PHP如下所示:

if( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['p']) && isset($_POST['message']) ){
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$p = $_POST['p'];
$email_from = 'default03@securemail.com';
$email_subject = "New Form submission $name ";
$email_body = "You have received a new message from the user $p.\n".  "Here is the message:\n $message".
$to = "erixom@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
}
else
echo " ISSET FAIL";
$email_from = 'default03@securemail.com';
$email_subject = "New Form submission $name ";
$email_body = "You have received a new message from the user $p.\n".  "Here is the message:\n $message".
$to = "test@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>

1 个答案:

答案 0 :(得分:2)

而不是id="name"您必须使用name="name"id属性用于JavaScript操作,而name属性用于命名POST个变量

我想补充一点,您应该清理用户提交的输入。即使这可能不是一个面向互联网的项目,它也永远不会太早采用这个作为习惯:)