php四个输入框只能输入数字如果不等于1111提示验证码错误后清空输入框等待重新输入如果等于跳转。编写个代码
以下是一个简单的PHP代码示例,用于验证四个输入框中的数字是否等于1111,如果不等于则提示错误并清空输入框,如果等于则跳转到指定页面。
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input1 = test_input($_POST["input1"]);
$input2 = test_input($_POST["input2"]);
$input3 = test_input($_POST["input3"]);
$input4 = test_input($_POST["input4"]);
if ($input1 == 1 && $input2 == 1 && $input3 == 1 && $input4 == 1) {
header("Location: http://www.example.com"); //跳转到指定页面
exit();
} else {
echo "验证码错误,请重新输入。";
$input1 = "";
$input2 = "";
$input3 = "";
$input4 = "";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = is_numeric($data) ? $data : ""; //检查输入是否为数字
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" name="input1" maxlength="1" size="1">
<input type="text" name="input2" maxlength="1" size="1">
<input type="text" name="input3" maxlength="1" size="1">
<input type="text" name="input4" maxlength="1" size="1">
<input type="submit" name="submit" value="提交">
</form>
``
原文地址: https://www.cveoy.top/t/topic/ecUr 著作权归作者所有。请勿转载和采集!