我的PHP表单不会向我发送电子邮件并重定向到空白页面

时间:2014-08-08 18:44:19

标签: php forms email

我已经研究了这个问题,但我认为我不会找到答案,因为我猜我的代码本身有问题。我尝试了将我的PHP联系表单提交给提交的所有组合。给我发一封电子邮件,但它什么也没做,即使已经上传到服务器了。一旦我点击提交,它只显示一个空白页面,我根本没有收到我输入代码的电子邮件地址的电子邮件。我不知道我错过了什么,因为我无法在任何地方找到问题的答案。

我注意到的一件事是我的HTML中有一些不同的字段,而不是PHP中指定的字段。但是,我不知道这会有所不同,因为我没有收到任何错误消息或其他任何这些字段。如果您需要任何其他详细信息,请与我们联系。我提前感谢您的帮助。

HTML

<form id="form_884913" method="post" action="send_form_email.php" class="appnitro">

这是我所有字段的所在,因此我省略了相当大的脚本。如果您需要这些帮助我,请告诉我

HTML的其余部分

<input type="hidden" name="form_id" value="884913" />               
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
<input type="reset" value="Reset">

    </form> 

PHP

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemailaddresswastyped@blank.com";
$email_subject = "Contact";


function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
 if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->

Thank you for contacting me! I will be in touch with you very soon.

<?php
 }
 ?>

3 个答案:

答案 0 :(得分:1)

正如GaijinJim所提到的,如果它已经不是您发布的字段,因为您省略了一些字段,请确保在HTML中添加字段:

<input type="hidden" name="email" value="" /> 

也可以添加测试,但不能用于生产,到PHP的最后一行:

<?php
    } else {
        echo "$_POST['email'] is not set.";
    }
?>

答案 1 :(得分:0)

如果您希望将$_POST(与$_GET方法相同)与HTML和PHP中的表单一起使用,则必须将您的字段命名为:

<input type="text" name="MyfieldName" value="1">

只有这样,您才能拨打$_POST['MyfieldName']

现在,如果你echo $_POST['MyfieldName'];它会返回1

答案 2 :(得分:0)

在PHP文件中,检查表单中是否存在以下所有输入字段,

// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
}

因此,为了使您的表单验证所有这些字段必须存在,您必须具有

<input type="text" name="first_name" />
<input type="text" name="last_name" /> 
<input type="text" name="email" /> 
<input type="text" name="telephone" /> 
<input type="text" name="comments" />

如果您不想要其中某些字段,例如电子邮件,只需从PHP的if语句中删除`!isset($ _ POST ['email'])。


根据您的评论,我得到的结果是名为email的输入字段不存在,这就是您获得空白页面的原因。如果您要在HTML页面上添加该输入字段,您会看到某种错误消息而不是空白页面:)