我刚刚开始使用PHP,我一直在尝试验证HTML表单,然后将表单的数据发布到另一个页面上,由于某种原因,这似乎并不令人满意想要工作问题是,当单击提交时,如果没有错误,页面只会刷新。以下是代码片段:
<?php
$nameErr = $surnameErr = " ";
$name = $surname = " ";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$valid = 0;
if (empty($_POST["name"])) {
$nameErr = "Name is required";
$valid++;
} 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["surname"])) {
$surnameErr = "Surname is required";
$valid++;
} else {
$surname = test_input($_POST["surname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
$surnameErr = "Only letters and white space allowed";
}
}
if($valid == 0){
header('LOCATION: page2.php');
exit();
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
这是HTML
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<div class="label1">
<label>First Name</label>
<input type="text" name="name" id="name" placeholder="John" value="<?php echo $name;?>" onblur="validateName('name')">
<label>Surname</label>
<input type="text" name="surname" id="surname" placeholder="Smith" value="<?php echo $surname;?>" onblur="validateSurname('surname')"> <br />
<input type="submit" name="submit" value="Submit">
</div>
</form>
使page2.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your surname is: <?php echo $_POST["surname"]; ?>
</body>
</html>
答案 0 :(得分:1)
执行header('LOCATION: page2.php');
后,您将丢失page2.php
中无法使用的所有已发布数据。
有几种解决方案,例如:
include
代替header
重定向; 我没有看到您在此处无法使用include
的任何理由,是否有您想要重定向的特定原因?
答案 1 :(得分:0)
试试这个
**main page**
<form method="post" action="page2.php">
<div class="label1">
<label>First Name</label>
<input type="text" name="name" id="name" placeholder="John" onblur="validateName('name')">
<label>Surname</label>
<input type="text" name="surname" id="surname" placeholder="Smith" onblur="validateSurname('surname')"> <br />
<input type="submit" name="submit" value="submit">
</div>
</form>
**page2.php**
<?php
if (isset($_POST['submit'])) {
$valid = 0;
if (empty($_POST["name"])) {
$name = "Name is required";
$valid++;
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$name = "Only letters and white space allowed";
}
else{$name=$_POST["name"];}
}
if (empty($_POST["surname"])) {
$surname = "Surname is required";
$valid++;
} else {
$surname = test_input($_POST["surname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
$surname = "Only letters and white space allowed";
} else{$surname=$_POST["surname"];}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<html>
<body>
Welcome <?=$name?><br>
Your surname is: <?=$surname?>
</body>
</html>