PHP电子邮件表单未发送所有字段

PHP电子邮件表单未发送所有字段,php,forms,email,submit,Php,Forms,Email,Submit,当我提交表格时,我收到的是姓氏和电子邮件字段,而不是名字。快把我逼疯了!如果有人能快速看一下,让我知道为什么我会非常感激。我是PHP的初学者。谢谢 HTML: 第一个名称: 电子邮件地址: 最后一次姓名: 以下是PHP: <?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "#"; $email_subject = "Email Upd

当我提交表格时,我收到的是姓氏和电子邮件字段,而不是名字。快把我逼疯了!如果有人能快速看一下,让我知道为什么我会非常感激。我是PHP的初学者。谢谢

HTML:


第一个名称:
电子邮件地址:
最后一次姓名:
以下是PHP:

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

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "#";
    $email_subject = "Email Update Sign-up Request from #";


    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']))


    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // 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 />';
  }

    $email_message = "Form details below.\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";



// 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 us. We will be in touch with you very soon. Please use your "Back Button" to return to the Home Page

<?php
}
?>

感谢您联系我们。我们将很快与您联系。请使用“后退按钮”返回主页

您的问题在于以下代码:

// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']))


$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
它是一个if语句,没有代码跟随,因此它将拾取下一行,在该行中分配$first\u name。所以这永远不会被执行。这相当于:

// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']))
{
    $first_name = $_POST['first_name']; // required
}

$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required

另一方面,一般来说,这段代码相当混乱,缩进级别不一致,等等。我知道这看起来很琐碎和迂腐,但如果你花多一点时间编写更干净的代码,你会发现这样的错误很容易排除(其他人也一样)。

很好,解决了它。非常感谢。我肯定会在将来清理代码——今天会非常匆忙。再次感谢您打印($\u POST);你得到了什么?对不起,这里全是新手。我应该把它放在哪里?在:
if(isset($\u POST['email'))
// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']))
{
    $first_name = $_POST['first_name']; // required
}

$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required