首页 > 解决方案 > 文件应该处理报告所有错误

问题描述

我对 PHP 相当陌生。我有一个任务要求我制作两个单独的文件。上调用input.html 和其他handle.php。

input.html 文件有一个 for 将接收用户输入,然后将其发送到 handle.php 文件。

input.html 应该看起来像 ,如果所有字段都被归档,那么我应该显示这个

在此处输入图像描述



在此处输入图像描述

我遇到的复杂情况是所需的文件。如果字段没有文本或未选中收音机,那么它应该警告用户,但我会一直将它们发送到 PHP 文件并显示错误。

我的input.html代码是

<!doctype html>
<html lang="en">

<head>
<title> Feedback Form </title>
<style>
.error {color: #FF0000;}
</style>
</head>

<body>

<?php
// define variables and set to empty values
$nameErr = $emailErr = $responseErr = $commentErr = "";
$name = $email = $response = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    //Check if name only contains letters and whitespace
    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"]);
    //Check if email address is well-formated
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $emailErr = "Invalid email format";
    }
  }

  if (empty($_POST["response"])) {
    $responseErr = "Response is required";
  } else {
    $response = test_input($_POST["response"]);
  }

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

}

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


<p>Please complete this form to submit your feedback:</p>

<p><span class="error">* required field</span></p>

<!-- Start of the form -->
<form method="post" action="handle.php">

Name:   <select name="prefix">
            <option>Mr. </option>
            <option>Mrs. </option>
            <option>Ms. </option>
        </select> <input type="text" name="name">
        <span class="error">* <?php echo $nameErr;?></span>
        <br><br>

Email Address: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>

Response: This is...
    <input type="radio" name="response" value="excellent">excellent
    <input type="radio" name="response" value="okay">okay
    <input type="radio" name="response" value="boring">boring
    <span class="error">* <?php echo $responseErr;?></span>
    <br><br>

Comments: <textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $commentErr;?></span>
<br><br>

<input type="submit" value="Send My Feedback">
</form>
<!-- End of the form -->






</body>

</html>



我的handle.php代码是

<html>
<head></head>
<body>

Thank you, <?php echo $_POST["prefix"] ?> <?php echo $_POST["name"]; ?> for your comment.<br><br>
You stated that you found this example to be '<?php echo $_POST["response"]; ?>'<br>
and added: <br>
'<?php echo $_POST['comment'];?>'<br>


</body>
</html>

因此,当我将所有字段留空而不是警告我它们为空白时,我得到了这个 ,我尝试使用w3shool作为指导线,但它不起作用。我真的很感激一些帮助,并感谢您花时间尝试帮助我的一切。:)
在此处输入图像描述

标签: phphtml

解决方案


你可以用它required来解决你的问题。如果必填字段为空或未选中,则required停止提交。form

<form method="post" action="handle.php">

    Name:
    <select name="prefix">
        <option>Mr. </option>
        <option>Mrs. </option>
        <option>Ms. </option>
    </select>
    <input type="text" name="name" required>
    <span class="error">* <?php echo $nameErr;?></span>
    <br>
    <br> Email Address:
    <input type="email" name="email" required>
    <span class="error">* <?php echo $emailErr;?></span>
    <br>
    <br> Response: This is...
    <input type="radio" name="response" value="excellent" required>excellent
    <input type="radio" name="response" value="okay" required>okay
    <input type="radio" name="response" value="boring" required>boring
    <span class="error">* <?php echo $responseErr;?></span>
    <br>
    <br> Comments:
    <textarea name="comment" rows="5" cols="40" required></textarea>
    <span class="error">* <?php echo $commentErr;?></span>
    <br>
    <br>

    <input type="submit" value="Send My Feedback">
</form>

我还建议使用“电子邮件”作为电子邮件输入字段的类型。这将确保输入数据是有效的电子邮件格式。


推荐阅读