我正在使用PHP表单将数据转发到电子邮件地址。一切似乎都运行正常,除非在输入用户的任何输入之前,在加载页面时出现错误消息[“您还没有输入电子邮件”],而不是在提交时通过验证。
表单在http://www.soulwatt.com/contact.php
注意:在搜索了如何将数据转发到电子邮件之后,我在网上发现了这个PHP代码,因此它不是我的。请原谅缺乏正确的代码格式。
<?php
$to = $_REQUEST['sendto'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "soulwatt.com Contact Data!!";
$fields = array();
$fields{"Name"} = "Name";
$fields{"Company"} = "Company";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"list"} = "Mailing List";
$fields{"Comments"} = "Comments";
$body = "Soul Watt has received the following information:\n\n";
foreach($fields as $a => $b) {
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$headers2 = "From: noreply@soulwatt.com";
$subject2 = "Thank you for contacting Soul Watt!";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.soulwatt.com";
if($from == '') {
print "You have not entered an email. Please enter your email and try again.";
}
else {
if($name == '') {
print "You have not entered a name.<br />Please enter your name and try again.";
}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send) {
print "<p><span>THANK YOU FOR CONTACTING US!</span></p>";
print "<p><span>Someone will get back to you as soon as possible, usually within 48 hours. If you need immediate assistance regarding booking Soul Watt, please call Randy at (828) 729-3199.</span></p>";
}
else {
print "<p><span>We encountered an error sending your mail, please notify webmaster@soulwatt.com</span></p>";
}
}
}?>
感谢您的帮助!
答案 0 :(得分:2)
这两行:
$from = $_REQUEST['Email'] ;
if($from == '') {
print "You have not entered an email. Please enter your email and try again.";
}
表示您检查了email
请求(POST
和GET
)密钥。第一次加载此页面时,这将是空的。您可以添加检查是否有POST
,例如,如果已提交。
老实说,您的代码可能存在很多问题:用户可以在其中添加各种内容,甚至可能在标题中添加内容以添加“to”字段和所有内容..您可能在这里制作垃圾邮件机器。这部分:$headers = "From: $from";
只是在您的标题中添加了请求字段FROM
....
答案 1 :(得分:2)
您希望将验证部分包装在
中if ($_SERVER['REQUEST_METHOD'] == 'POST') {
... do validation here ...
}
... print form here ...
这只会在您提交表单后运行验证码。现在,它每次加载页面时都在运行,所以当然没有表单数据可以在第一次验证。
答案 2 :(得分:0)
您的表单字段是否与您正在检查的名称相同?
我的意思是
$from = $_REQUEST['Email'];
你应该
<input type='text' name='Email' /> <!-- Note the Capital E -->
我在这里添加我的评论:
使用$ _POST或$ _GET。避免$ _REQUEST。通过这种方式,您可以更好地控制您的应用。
另外,不要使用==“”检查空虚(尝试空($ from)
答案 3 :(得分:0)
好的,如果表单在http://www.soulwatt.com/contact.php上,你有
<form method="post" action="contact.php" name="contact_form" id="contact_form">
action =“contact.php”:这意味着它正在同一地址处理表单,因此您将在同一页面上看到表单处理的结果,并且因为表单是空的,并且您正在检查它无论是否已发布,您都会收到该错误