Php 联系人表单仅发送消息正文,而忽略其他字段

Php 联系人表单仅发送消息正文,而忽略其他字段,php,html,Php,Html,请大家注意,我遇到了很大的麻烦,在过去的24小时里我一直在流汗,我有这张我上传到服务器上的联系表,但令人伤心的是,它只是将邮件正文发送到我的电子邮件,而忽略了其他字段,如姓名字段、电子邮件字段和电话号码。我厌倦了盯着php代码看,我觉得一切都很好,但代码没有按预期工作,请帮助我 以下是我的php代码: <?php // define variables and set to empty values $name_error = $email_error = $phone_error =

请大家注意,我遇到了很大的麻烦,在过去的24小时里我一直在流汗,我有这张我上传到服务器上的联系表,但令人伤心的是,它只是将邮件正文发送到我的电子邮件,而忽略了其他字段,如姓名字段、电子邮件字段和电话号码。我厌倦了盯着php代码看,我觉得一切都很好,但代码没有按预期工作,请帮助我

以下是我的php代码:

<?php 

// define variables and set to empty values
$name_error = $email_error = $phone_error = $url_error = "";
$name = $email = $phone = $message = $url = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $name_error = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $name_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }

  if (empty($_POST["phone"])) {
    $phone_error = "Phone is required";
  } else {
    $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
    if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
      $phone_error = "Invalid phone number"; 
    }
  }

  if (empty($_POST["url"])) {
    $url_error = "";
  } else {
    $url = test_input($_POST["url"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url)) {
      $url_error = "Invalid URL"; 
    }
  }

  if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }

  if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == '' ){
      $message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $message_body .=  "$key: $value\n";
      }

      $to = 'emmanuelgbnn23@gmail.com';
      $subject = 'Contact Form Submit';
      if (mail($to, $subject, $message)){
          $success = "Message sent, thank you for contacting us!";
          $name = $email = $phone = $message = $url = '';
      }
  }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

更改代码

if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == '' )
对此

if ($name_error == '' && $email_error == '' && $phone_error == '' && $url_error == '' )
学习php操作符更改代码

if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == '' )
对此

if ($name_error == '' && $email_error == '' && $phone_error == '' && $url_error == '' )

了解php运算符

您在mail()函数中使用了错误的变量
$message
。由于要将值追加到脚本中的
$message\u body
中,请将
$message
替换为
$message\u body
,然后重试

<?php 

    // define variables and set to empty values
$name_error = $email_error = $phone_error = $url_error = "";
$name = $email = $phone = $message = $url = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $name_error = "Name is required";
    } else {
        $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $name_error = "Only letters and white space allowed"; 
        }
    }

    if (empty($_POST["email"])) {
        $email_error = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $email_error = "Invalid email format"; 
        }
    }

    if (empty($_POST["phone"])) {
        $phone_error = "Phone is required";
    } else {
        $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
        if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
            $phone_error = "Invalid phone number"; 
        }
    }

    if (empty($_POST["url"])) {
        $url_error = "";
    } else {
        $url = test_input($_POST["url"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url)) {
            $url_error = "Invalid URL"; 
        }
    }

    if (empty($_POST["message"])) {
        $message = "";
    } else {
        $message = test_input($_POST["message"]);
    }

    if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == '' ){
        $message_body = '';
        unset($_POST['submit']);
        foreach ($_POST as $key => $value){
            $message_body .=  "$key: $value\n";
        }

        $to = 'emmanuelgbnn23@gmail.com';
        $subject = 'Contact Form Submit';
        if (mail($to, $subject, $message_body)){
            $success = "Message sent, thank you for contacting us!";
            $name = $email = $phone = $message = $url = '';
        }
    }

}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

?>

您在mail()函数中使用了错误的变量
$message
。由于要将值追加到脚本中的
$message\u body
中,请将
$message
替换为
$message\u body
,然后重试

<?php 

    // define variables and set to empty values
$name_error = $email_error = $phone_error = $url_error = "";
$name = $email = $phone = $message = $url = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $name_error = "Name is required";
    } else {
        $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $name_error = "Only letters and white space allowed"; 
        }
    }

    if (empty($_POST["email"])) {
        $email_error = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $email_error = "Invalid email format"; 
        }
    }

    if (empty($_POST["phone"])) {
        $phone_error = "Phone is required";
    } else {
        $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
        if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
            $phone_error = "Invalid phone number"; 
        }
    }

    if (empty($_POST["url"])) {
        $url_error = "";
    } else {
        $url = test_input($_POST["url"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url)) {
            $url_error = "Invalid URL"; 
        }
    }

    if (empty($_POST["message"])) {
        $message = "";
    } else {
        $message = test_input($_POST["message"]);
    }

    if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == '' ){
        $message_body = '';
        unset($_POST['submit']);
        foreach ($_POST as $key => $value){
            $message_body .=  "$key: $value\n";
        }

        $to = 'emmanuelgbnn23@gmail.com';
        $subject = 'Contact Form Submit';
        if (mail($to, $subject, $message_body)){
            $success = "Message sent, thank you for contacting us!";
            $name = $email = $phone = $message = $url = '';
        }
    }

}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

?>



print\r($\u POST)
?替换
if($name\u error=''和$email\u error=''和$phone\u error=''和$url\u error=''){
if($name\u error=''&$email\u error=''&&$phone\u error=''&$url\u error=''){
请不要问@Gunaseelan这个问题,我试过了,但没用在
foreach
之后,通过
var\u dump($message\u body)
检查
$message\u body
值。如果($name\u error=''和$email\u error=''和$phone\u error=''和$url\u error='',你在
中得到了什么{
如果($name\u error=''&&$email\u error=''&$phone\u error=''&&&$url\u error='',{
请告诉我,我没有得到问题@Gunaseelan,我试过了,但在
foreach
之后它不起作用,检查
$message\u body
值,方法是
变量转储($message\u body)
$message有数据,问题在于if条件下的和操作员。请注意,他的问题是“联系人表单仅发送消息正文,而忽略其他字段”@PPP你在$message_body中得到值了吗?只需打印和检查。脚本仍然只发送消息字段,但忽略了其他字段,伙计们请帮助。请作为php初学者,不要真正理解你要求我做的事情$message有数据,问题在于if条件下的和运算符。请注意,他的问题是“联系人表单仅发送邮件正文,而忽略其他字段"@PPP您是否在$message_body中获得了价值?请打印并检查。脚本仍然只发送消息字段,但忽略了其他字段,伙计们,请帮助。请作为php初学者,不要真正理解您要我做的事情。
&&
之间有什么区别?除了它们有不同的先例之外ce,在这种特定情况下不重要。那么
&&
之间的区别是什么?除了它们具有不同的优先级,在这种特定情况下不重要。