简单的PHP表单错误

时间:2014-08-05 18:36:28

标签: php forms

我正在尝试创建一个简单的PHP电子邮件表单,我可以将其嵌入到我的网站中。我有两个问题,语法似乎错过了href = \“\”>而且我不确定为什么。文本的其余部分显示在浏览器之后,即href = \“\”>部分。如果我使用此表单,是否更容易让用户无法在此PHP表单中看到我的电子邮件?请注意,我是PHP新手

<?php 
$action=$_REQUEST['action']; 
if ($action=="")    /* display the contact form */ 
    { 
    ?> 
<form  action="" method="POST" enctype="multipart/form-data"> 
<input type="hidden" name="action" value="submit">
Your email:<br> 
<input name="email" type="text" value="" size="30"/><br> 
Your message:<br> 
<textarea name="message" rows="7" cols="30"></textarea><br> 
<input type="submit" value="Send email"/> 
</form> 
<?php 
}  
else                /* send the submitted data */ 
{ 
$email=$_REQUEST['email']; 
$message=$_REQUEST['message']; 
if (($name=="")||($email=="")||($message=="")) 
    { 
    echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
    } 
else{         
    $from="From: $name<$email>\r\nReturn-path: $email"; 
    $subject="Message sent using your contact form"; 
    mail("me@email.org", $subject, $message, $from); 
    echo "Email sent!"; 
    } 
}   
?> 

看错了什么?

1 个答案:

答案 0 :(得分:1)

您正在检查$name并且它不存在。这将使您工作,但您应该在定义之前检查$_REQUEST是否存在。始终编​​写带有错误报告的代码。

<?php 
$action=$_REQUEST['action']; 
if ($action=="")    /* display the contact form */ 
    { 
    ?> 
<form  action="" method="POST" enctype="multipart/form-data"> 
<input type="hidden" name="action" value="submit">
Your email:<br> 
<input name="email" type="text" value="" size="30"/><br> 
Your message:<br> 
<textarea name="message" rows="7" cols="30"></textarea><br> 
<input type="submit" value="Send email"/> 
</form> 
<?php 
}  
else                /* send the submitted data */ 
{ 
$email=$_REQUEST['email']; 
$message=$_REQUEST['message']; 
if (($email=="")||($message=="")) 
    { 
    echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
    } 
else{         
    $from="From: $name<$email>\r\nReturn-path: $email"; 
    $subject="Message sent using your contact form"; 
    mail("me@email.org", $subject, $message, $from); 
    echo "Email sent!"; 
    } 
}   
?>