php - 结果显示未经验证
问题描述
我正在制作一个测验/madlibs 类的网站,所以除了验证之外一切正常。每当用户输入特殊字符或将输入字段留空时,都应该有一个警告,并且不应允许提交按钮显示最终结果。
现在当我输入例如“?” 然后按下提交按钮,它将显示所有答案的最终结果。
我的验证做错了什么?我怎样才能做到这一点,这样你就不能把它留空并使用特殊字符。
我使用以下代码检查验证:
<?php
$Input1 = $Input2 = $Input3 = $Input4 = $Input5 = $Input6 = $Input7 = "";
$Input1Err = $Input2Err = $Input3Err = $Input4Err = $Input5Err = $Input6Err = $Input7Err = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST["Input1"])){
$Input1Err = "enter something.";
}else{
$Input1 = test_input($_POST["Input1"]);
if(!preg_match("/[^a-z0-9 _]+$/i", $Input1)){
$Input1Err = "only letters and white space allowed";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
} ?>
对于每个输入,我都进行了这样的验证:
if(empty($_POST["Input1"])){
$Input1Err = "enter something.";
}else{
$Input1 = test_input($_POST["Input1"]);
if(!preg_match("/[^a-z0-9 _]+$/i", $Input1)){
$Input1Err = "only letters and white space allowed";
}
}
这是完整的html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Mad Libs</title>
<link rel="stylesheet" type="text/css" href="Site.css">
</head>
<body>
<h1 class="text">Quiz game</h1>
<div class="container">
<nav>
<ul>
<li><a href="Quiz.php">quiz</a></li>
</ul>
</nav>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<h1> quiz</h1>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$Input1 = $_POST["Input1"];
$Input2 = $_POST["Input2"];
$Input3 = $_POST["Input3"];
$Input4 = $_POST["Input4"];
$Input5 = $_POST["Input5"];
$Input6 = $_POST["Input6"];
$Input7 = $_POST["Input7"];
echo "Driving a car can be fun if you follow this $Input1. advice:
When approaching a $Input2 on the right, always blow your $Input4 Before making a
$Input3 turn, always stick your $Input2 out of the window.
Every 2000 miles, have your $Input1.inspected and your $Input5 checked.
When approaching a school, watch out for $Input6.
Above all, drive $Input6 the $Input7 you save may be your own!$Input2";
?>
<?php }else{ ?>
<p>Question.. <input type="text" name="Input1" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input2" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input3" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input4" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input5" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input6" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input7" placeholder="enter here"></p>
<input type="submit" name="submit" value="Send">
</form>
<?php } ?>
<footer>
<p>@2021.</p>
</footer>
</div>
</body>
</html>
解决方案
我认为您不必验证输入是否是 php.ini 中带有空格的文本。您可以使用 html 输入模式属性。例如:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
<label for="maininput">Input</label>
<input type="text" id="maininput" name="maininput" pattern="[A-Za-z\s]+" title="Write Here your Error"><br>
<input type="submit">
</form>
</body>
</html>
在此示例中,输入仅接受字母和空格。您还可以通过将模式更改为 [A-Za-z0-9\s]+ 来添加数字。另外,如果您真的想继续使用 php 来验证输入,我认为错误是行中的验证if(!preg_match("/[^a-z0-9 _]+$/i", $Input1)){
。
推荐阅读
- python - 在 Python 中继承记录器名称
- javascript - Jest Snapshots 在 SVG 道具上失败
- random - 从 Google 表格列表中选择随机单元格的简单方法
- python - Tkinter 单选按钮控制是否为真
- tfs - 将代码从 TFS 2010 上的 TFVC 移动到 Azure DevOps 服务器上的 TFVC
- python - 有效地合并两个迭代器
- html - 图像在 img 标签中旋转
- python - 使用 np.where 创建具有三个条件的新列
- swift - SwiftUI - 日期选择器未正确显示
- gnuplot - 用 gnuplot 绘制表面,但我只有一个 Z 数据数据文件