为输入用户设置一些指导方针,我相信我的语法和编码是正确的,但也许我没有正确引用我的第二个php文件?我是php的新手,所以我的第一直觉就是为我的文本框使用onfocus或onblur函数,但这样做是错误的,所以任何帮助都会非常有用。感谢。
的index.php
<span class ="error"> * required field </span>
<form name ="myForm" action ="display.php<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"
method = "POST"><br>
Name: <input type="text" name="userName" value ="<?php echo $name;?>"
<span class ="error"> *<?php echo $nameErr;?></span><br>
University: <input type="text" name="userUniv" value ="<?php echo $university;?>"
<span class ="error"> *<?php echo $univErr;?></span><br>
Email: <input type ="text" name ="userEmail" value ="<?php echo $email;?>"
<span class ="error"> *<?php echo $emailErr;?></span><br><br>
Display.php的
<?php
$nameErr = $emailErr = $univErr = "";
$name = $email = $university = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["userName"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["userName"]);
if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
$nameErr = "Invalid name";
}
}
if (empty($_POST["userEmail"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["userEmail"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["userUniv"])) {
$univErr = "University is required";
} else {
$university = test_input($_POST["userUniv"]);
if (!preg_match("/^[a-zA-Z ]*$/", $university)) {
$univErr = "Invalid University";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
答案 0 :(得分:0)
当您的表单操作为display.php
时,您将从表单重定向,因此它不再引用该布局,您可以通过几种方式显示数据。将操作作为当前页面并在其中执行所有逻辑或将错误消息保存到会话中,并将页面重定向回到表单并通过ajax显示或验证。
我将展示前者。此外,您还要删除$_SERVER['PHP_SELF']
,这会使您的操作类似于display.php/display.php
,假设它位于根目录中。
<?php
$nameErr = $emailErr = $univErr = "";
$name = $email = $university = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["userName"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["userName"]);
if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
$nameErr = "Invalid name";
}
}
if (empty($_POST["userEmail"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["userEmail"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["userUniv"])) {
$univErr = "University is required";
} else {
$university = test_input($_POST["userUniv"]);
if (!preg_match("/^[a-zA-Z ]*$/", $university)) {
$univErr = "Invalid University";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<span class ="error"> * required field </span>
<form name="myForm" action="" method = "POST">
<br>
Name: <input type="text" name="userName" value ="<?php echo $name;?>"
<span class ="error"> *<?php echo $nameErr;?></span><br>
University: <input type="text" name="userUniv" value ="<?php echo $university;?>"
<span class ="error"> *<?php echo $univErr;?></span><br>
Email: <input type ="text" name ="userEmail" value ="<?php echo $email;?>"
<span class ="error"> *<?php echo $emailErr;?></span><br><br>
<input type="submit" value="submit me">
</form>