此脚本每次"Please fill out all the mandatory fields."都会向我显示此消息



我正在尝试所有内容,但是此PHP脚本显示此错误"请填写所有强制性字段。"

请帮助我解决这个问题。

<?php
session_start();
if (isset($_POST['fullname']) &&  isset($_POST['email']) && isset($_POST['phone'])) {
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $headers = "MIME-Version: 1.0"."rn";
    $headers.= "Content-type:text/html;charset=UTF-8"."rn";
  $headers.= 'From: <'.$email.'>'."rn";
  $mailto = "google@gmail.com";
  $subject = "Web Design & Development Service";
  $msg2send = "Hi $fullname,
  Hi, we have received one fresh query for you.
  Name: $fullname
  Email: $email
  Phone: $phone ";
  $msg2send = nl2br($msg2send);
  if (mail($mailto, $subject, $msg2send, $headers)) {
      echo "Thanks for writing to us. We will get back to you as soon as possible.";
  } else {
      echo "Please fill out all the mandatory fields.";
  }
} else {
    echo "Your enquiry could not be sent for some reason; please try sending us again.";
}
?>

您的 if else语句定位不正确。

他们指出了错误的条件。

重新排列的代码:

<?php
session_start();
if (isset($_POST['fullname']) &&  isset($_POST['email']) && isset($_POST['phone'])) {
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $headers = "MIME-Version: 1.0"."rn";
    $headers.= "Content-type:text/html;charset=UTF-8"."rn";
  $headers.= 'From: <'.$email.'>'."rn";
  $mailto = "google@gmail.com";
  $subject = "Web Design & Development Service";
  $msg2send = "Hi $fullname,
  Hi, we have received one fresh query for you.
  Name: $fullname
  Email: $email
  Phone: $phone ";
  $msg2send = nl2br($msg2send);
  if (mail($email, $subject, $msg2send, $headers)) {
      echo "Thanks for writing to us. We will get back to you as soon as possible.";
  } else {
    // #1: Swipe with #2
      echo "Your enquiry could not be sent for some reason; please try sending us again."; // Flip this with #2
  }
} else {
  echo "Please fill out all the mandatory fields."; // #2
}

?>

如果语句是一种不好的做法,则太多了,因为很难调试。相反,您应该将它们分解为小陈述

例如:

<?php
session_start();
if !(isset($_POST['fullname']) ||  isset($_POST['email']) || isset($_POST['phone'])) {
    echo "Your enquiry could not be sent for some reason; please try sending us again.";
    exit();
}
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."rn";
$headers.= "Content-type:text/html;charset=UTF-8"."rn";
$headers.= 'From: <'.$email.'>'."rn";
$mailto = "google@gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($mailto, $subject, $msg2send, $headers)) {
  echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
  echo "Please fill out all the mandatory fields.";
}
?>

现在,代码变得更清晰,您可以追溯。看来您的代码在邮件函数时有错误,因此返回false,然后显示邮件。

最新更新