文件应处理报告所有错误

时间:2018-10-01 03:54:53

标签: php html5

我在PHP上还很陌生。我的作业要求我制作两个单独的文件。在名为input.html和另一个handle.php上。

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

input.html应该看起来像

enter image description here

 如果所有字段都填写了,那么我应该显示此

enter image description here

我遇到的麻烦是必需的文件。如果字段中没有文本或未选中广播,则它应该警告用户,但与我合作,它会继续将其发送到PHP文件并显示错误。

我的 input.html 代码是

//get path to external storage (SD card)
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/Abc/";
File sdIconStorageDir = new File(iconsStoragePath);

//create storage directories, if they don't exist

if(!sdIconStorageDir.exists()){
   sdIconStorageDir.mkdirs();
}



,而我的 handle.php 代码是

<!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>

因此,当我将所有字段都保留为空白而不是警告我它们为空白时,我得到了这个
enter image description here
我尝试使用w3shool作为指导,但是没有用。我非常感谢您的帮助,并感谢您花费他们所有时间尝试帮助我的事情。 :)

1 个答案:

答案 0 :(得分:1)

您可以使用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>

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

相关问题