php - 文件应该处理报告所有错误
问题描述
我对 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作为指导线,但它不起作用。我真的很感激一些帮助,并感谢您花时间尝试帮助我的一切。:)
解决方案
你可以用它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>
我还建议使用“电子邮件”作为电子邮件输入字段的类型。这将确保输入数据是有效的电子邮件格式。
推荐阅读
- r - 从 R 会话连接到 Amazon AWS PostgreSQL 数据库
- jquery - 检查 div 是否设置了背景图片
- arrays - []byte{10} 或 []byte("\n") 与 []byte{92, 110}
- c++ - 如何编辑文本文件中的特定行?
- java - 使用 mockito 具有 CompletableFuture.allOf 的测试方法
- multithreading - 为什么在子例程昂贵的情况下多线程执行不起作用?
- python-3.x - 关于定义函数和字符串列表的问题
- python - 如何将一列添加到 df 以汇总 A 列中条目的所有匹配值?
- swift - 如何在swiftUI中制作一堆垂直滑块
- saml - 如何修复在 Hue 应用程序中使用 SAML 的错误