Php表单不显示错误消息

时间:2018-03-27 06:59:16

标签: php forms validation verification

我正在尝试创建一个检查和验证名称,电子邮件的表单。但是我看不到任何错误消息。我不知道很多PHP,不能说我甚至都知道基础知识。

以下是代码:

<iframe name="formDestination" class="nnn"></iframe>
<div class="container33">
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="thwid" method="post"  target="formDestination">
            <label for="fname">Full Name</label>
            <input type="text" id="fname" name="firstname" placeholder="Your full name..." value="<?php echo $name;?>"><span class="error">* <?php echo $nameErr;?></span>
            <label for="email">Your E-mail</label>
            <input type="text" id="email" name="email" placeholder="Your E-mail adress..."> <span class="error">* <?php echo $emailErr;?></span>

            <label for="message">Your message</label>
            <textarea id="message" name="message" placeholder="Write your message here / the reason why you want to contact us " ></textarea>

            <input type="submit" name="submit" value="Submit">
        </form>
    </div>


<?php 
if(isset($_POST['submit'])){
$to = "myemail@cencored.com"; 
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
} ?>
<?php
   $nameErr = $emailErr = "";
   $name = $email = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

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

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

4 个答案:

答案 0 :(得分:1)

将表单HTML代码移动到所有PHP代码下面,否则您的错误变量(例如 $ emailErr )将不会显示,因为它们在使用之前未定义。

答案 1 :(得分:0)

试试这个。

<?php
   $nameErr = $emailErr = "";
   $name = $email = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (isset($_POST["firstname"]) && $_POST["firstname"] != "") {

    $name = test_input($_POST["firstname"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
      echo $nameErr;
    }

    } else {
       $nameErr = "Name is required";
       echo $nameErr;
  }

  if (isset($_POST["email"]) && $_POST["email"] != '') {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
    echo $emailErr;

  } else {
    $emailErr = "Email is required";
    echo $emailErr;
  }

  if (isset($_POST["comment"]) && $_POST["comment"] != '') {
     echo $comment;
     $comment = test_input($_POST["comment"]);
  } else {
     $comment = "";
     echo $comment;
  }

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

如果出现任何错误,您需要回显该值。

提示:始终使用isset检查值是否已设置。也可以在电子邮件功能中执行相同操作。

答案 2 :(得分:0)

将代码块重新放置在适当的位置。还删除了不需要的代码。

尝试:

Product existing = productRepo.findById(prod.getId());
existing.getStock().setCount(+prod.getStock().getCount());
productRepo.save(existing);

答案 3 :(得分:0)

<?php
   $nameErr = $emailErr = "";
   $name = $email = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

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

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
}?>
<div class="container33">
  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="thwid" method="post"  target="">
      <label for="fname">Full Name</label>
      <input type="text" id="fname" name="firstname" placeholder="Your full name..." value="<?php echo $name;?>">
      <span class="error">* <?php echo $nameErr;?></span>
      <br>

      <label for="email">Your E-mail</label>
      <input type="text" id="email" name="email" placeholder="Your E-mail adress..."> 
      <span class="error">* <?php echo $emailErr;?></span>  
      <br>

      <label for="message">Your message</label>
      <textarea id="message" name="message" placeholder="" ></textarea>

      <input type="submit" name="submit" value="Submit">
  </form>
</div>