PHP表单验证无法正常工作
我是PHP新手,正在上夜校课程。我们有一个表单验证项目。我有下面的代码,但当我单击submit时,它只是重定向到所需的站点,而没有执行所需的验证PHP表单验证无法正常工作,php,forms,validation,Php,Forms,Validation,我是PHP新手,正在上夜校课程。我们有一个表单验证项目。我有下面的代码,但当我单击submit时,它只是重定向到所需的站点,而没有执行所需的验证 <?php $checkedMale = $_POST['gender'] == 'Male' ? "checked='checked'" : ''; $checkedFemale = $_POST['gender'] != 'Male' ? "checked='checked'" : ''; $formValidates = fal
<?php
$checkedMale = $_POST['gender'] == 'Male' ? "checked='checked'" : '';
$checkedFemale = $_POST['gender'] != 'Male' ? "checked='checked'" : '';
$formValidates = false;
if(isset($_POST['submit']) && $_POST['submit'] == 'Register') {
$errors = array();
if ( $_POST['firstName'] == '') {
$errors[] = '<p>Please fill in your first name</p>';
}
if ( $_POST['surname'] == '') {
$errors[] = '<p>Please fill in your surname</p>';
}
if ( $_POST['email'] == '') {
$errors[] = '<p>Please fill in your e-mail address</p>';
} else {
if ( ! filter_var ($_POST['email'],FILTER_VALIDATE_EMAIL) ) {
$errors[] = "<p>Please supply a valid e-mail address</p>";
}
}
if ( $_POST['address'] == '') {
$errors[] = '<p>Please fill in your postal address</p>';
}
if (count($errors)== 0) {
$formValidates = true;
}
}
if ( ! $formValidates) {
// Displays errors
if (count($errors) > 0 ) {
echo "\n<ul>";
foreach ($errors as $error){
echo "\n\t<li>$error</li>";
}
echo "\n<ul>";
}
?>
<form name="questions" action="lesson8_1.php" method="post">
<table>
<tr>
<th>Title:</th>
<td>
<select name="title">
<option value="">Select</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Dr</option>
</select>
</td>
</tr>
<tr>
<th>First name:</th>
<td><input type="text" name="firstName" placeholder="First Name..." value="" /></td>
</tr>
<tr>
<th>Surname:</th>
<td><input type="text" name="surname" placeholder="Surname..." value="" /></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="email" placeholder="E-mail Address..." value="" /></td>
</tr>
<tr>
<th>Address:</th>
<td><textarea name="address" placeholder="Postal Address..."></textarea></td>
</tr>
<tr>
<th>Gender:</th>
<td>
<input type="radio" name="gender" value="Male" <?php echo $checkedMale?> >Male<br>
<input type="radio" name="gender" value="Female" <?php echo $checkedFemale?> >Female<br>
</td>
</tr>
<tr>
<th></th>
<td>
<input type='checkbox' name='option[]' value='Car'
<?php echo in_array('Car', $_POST['option']) ? 'checked' : '' ?>>I have a Car licence<br>
<input type='checkbox' name='option[]' value='Motorcycle'
<?php echo in_array('Motorcycle', $_POST['option']) ? 'checked' : '' ?>>I have a Motorcycle licence<br>
<input type='checkbox' name='option[]' value='Fishing'
<?php echo in_array('Fishing', $_POST['option']) ? 'checked' : '' ?>>I have a Fishing licence<br>
<input type='checkbox' name='option[]' value='TV'
<?php echo in_array('TV', $_POST['option']) ? 'checked' : '' ?>>I have a TV licence<br>
<input type='checkbox' name='option[]' value='Dog'
<?php echo in_array('Dog', $_POST['option']) ? 'checked' : '' ?>>I have a Dog licence<br>
</td>
</tr>
<tr>
<th></th>
<td><input type="submit" name="submit" value="Register" /></td>
</tr>
</table>
</form>
<?php } else { ?>
<h1>Your form has been successfully submitted!</h1>
<?php } ?>
标题:
挑选
先生
夫人
错过
博士
名字:
姓:
电邮:
地址:
性别:
>女性
>我有摩托车执照
>我有电视牌照
您的表单已成功提交!
如果您的php文件名为:“lesson8_1.php”,则表单工作正常。有一些未定义的变量,但是当您提交它时,它会返回所需的验证(我正在本地测试)
- 请填上你的名字
- 请填上你的姓
- 请填写您的电子邮件地址
- 请填写你的邮政地址 标题:
<input .... value="<?php if(isset($_POST['firstName'])){ echo $_POST['firstName']; } ?>" />
标题:
挑选
先生
夫人
错过
博士
名字:
OpenTag丢失。请查看其他一些验证后如何提交php表单的示例。谢谢,该文件名为lesson8_1.php。但是,如果我在表单为空的情况下单击submit,它仍然会重定向到页面,而不会验证任何内容。知道为什么吗?我复制粘贴了你的代码,并在我的电脑中按原样执行,效果很好。我添加了一行错误报告(0)
不希望看到警告,但当我提交表单时,它会重定向到同一页面并正确输出验证错误。你能解释一下情况吗?我猜页面重定向得很好。如果我将“名字”字段留空,我希望表单保留在lesson8.php上,并显示错误消息,即请输入您的名字。当我在字段为空/为空的情况下单击submit时,它仍然会重定向到lesson8_1.php,只有一些字段已完成/填写。Ok。您的文件必须命名为“lesson8.php”,并且表单操作应为:“lesson8.php”。然后,在提交时,表单将调用相同的文件并进行验证。如果一切正常,您可以重定向到带有标题('Location:…')的“lesson8_1.php”代码>。这不是验证表单的最佳方法,但它应该有效。基本上,我不确定我是否已经清楚地解释了自己。本质上,lesson8_1.php只是一个页面,它只是对表单结果的回应。是标题('位置:…');可以这样做吗?我应该在哪里添加代码?
<?php
$checkedMale = $_POST['gender'] == 'Male' ? "checked='checked'" : '';
$checkedFemale = $_POST['gender'] != 'Male' ? "checked='checked'" : '';
$formValidates = false;
if(isset($_POST['submit']) && $_POST['submit'] == 'Register') {
$errors = array();
if (!isset($_POST['firstName']) || $_POST['firstName'] == '') {
$errors[] = '<p>Please fill in your first name</p>';
}
if (!isset($_POST['surname']) || $_POST['surname'] == '') {
$errors[] = '<p>Please fill in your surname</p>';
}
if (!isset($_POST['email']) || $_POST['email'] == '') {
$errors[] = '<p>Please fill in your e-mail address</p>';
} else {
if ( ! filter_var ($_POST['email'],FILTER_VALIDATE_EMAIL) ) {
$errors[] = "<p>Please supply a valid e-mail address</p>";
}
}
if (!isset($_POST['address']) || $_POST['address'] == '') {
$errors[] = '<p>Please fill in your postal address</p>';
}
if (count($errors)== 0) {
$formValidates = true;
}
}
if ( ! $formValidates) {
// Displays errors
if (count($errors) > 0 ) {
echo "\n<ul>";
foreach ($errors as $error){
echo "\n\t<li>$error</li>";
}
echo "\n<ul>";
}
?>
<form name="questions" action="lesson8.php" method="post">
<table>
<tr>
<th>Title:</th>
<td>
<select name="title">
<option value="">Select</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Dr</option>
</select>
</td>
</tr>
<tr>
<th>First name:</th>
<td><input type="text" name="firstName" placeholder="First Name..." value="<?php if(isset($_POST['firstName'])){ echo $_POST['firstName']; }?>" /></td>
</tr>
<tr>
<th>Surname:</th>
<td><input type="text" name="surname" placeholder="Surname..." value="<?php if(isset($_POST['surname'])){ echo $_POST['surname']; }?>" /></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="email" placeholder="E-mail Address..." value="<?php if(isset($_POST['email'])){ echo $_POST['email']; }?>" />" /></td>
</tr>
<tr>
<th>Address:</th>
<td><textarea name="address" placeholder="Postal Address..."></textarea></td>
</tr>
<tr>
<th>Gender:</th>
<td>
<input type="radio" name="gender" value="Male" <?php echo $checkedMale?> >Male<br>
<input type="radio" name="gender" value="Female" <?php echo $checkedFemale?> >Female<br>
</td>
</tr>
<tr>
<th></th>
<td>
<input type='checkbox' name='option[]' value='Car'
<?php echo in_array('Car', $_POST['option']) ? 'checked' : '' ?>>I have a Car licence<br>
<input type='checkbox' name='option[]' value='Motorcycle'
<?php echo in_array('Motorcycle', $_POST['option']) ? 'checked' : '' ?>>I have a Motorcycle licence<br>
<input type='checkbox' name='option[]' value='Fishing'
<?php echo in_array('Fishing', $_POST['option']) ? 'checked' : '' ?>>I have a Fishing licence<br>
<input type='checkbox' name='option[]' value='TV'
<?php echo in_array('TV', $_POST['option']) ? 'checked' : '' ?>>I have a TV licence<br>
<input type='checkbox' name='option[]' value='Dog'
<?php echo in_array('Dog', $_POST['option']) ? 'checked' : '' ?>>I have a Dog licence<br>
</td>
</tr>
<tr>
<th></th>
<td><input type="submit" name="submit" value="Register" /></td>
</tr>
</table>
</form>
<?php } else { ?>
<h1>Your form has been successfully submitted!</h1>
<!-- here you can redirect with php to the desired location -->
<?php } ?>