我正在尝试创建一个PHP验证表单。这里有两个问题:
- 验证电子邮件输入
- 验证日期输入
由于某种原因,它没有捕捉到这两个。这是form。php
<div class="form_reg">
<h2>Registration Form</h2>
<form action="registration_process.php" method="post">
<p><label for="email">Email:</label>
<input type="email" name="email" value=""/></p>
<p><label for="first_name">First Name:</label>
<input type="text" name="first_name" value=""/></p>
<p><label for="last_name">Last Name:</label>
<input type="text" name="last_name" value=""/></p>
<p><label for="password">Password:</label>
<input type="password" name="password" value=""/></p>
<p><label for="confirm_password">Confirm Password:</label>
<input type="password" name="confirm_password" value=""/></p>
<p><label for="date">Birth Date:</label>
<input type="date" name="date" value=""/></p>
<input type="submit"/>
</form>
这是我的validate.php:
session_start();
$errors = array();
//empty array to collect errors
if(empty($_POST['email']) AND !filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{
$errors[] = "email cannot be blank";
}
if(empty($_POST['first_name']))
{
$errors[] = "First Name cannot be blank";
}
if(empty($_POST['last_name']))
{
$errors[] = "Last Name cannot be blank";
}
if(empty($_POST['password']))
{
$errors[] = "Password cannot be blank";
}
if(empty($_POST['confirm_password']) AND $_POST['password'] == $_POST['confirm_password'])
{
$errors[] = "Please enter matching password";
}
if(empty($_POST['confirm_password']) AND $_POST['password'] == $_POST['confirm_password'])
{
$errors[] = "Please enter matching password";
}
if(isset($_POST['date']) && strtotime($_POST['date']))
{
$errors[] = "Birth Date cannot be blank";
}
if(!empty($errors))
{
//if there are errors, assign the session variable!
$_SESSION['errors'] = $errors;
//redirect your user back using header('location: ')
header('Location: registration_page.php');
}
else
{
$email = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$password = $_POST['password'];
$birth_date = $_POST['date'];
//redirect your user to the next part of the site!
}
?>
任何想法?
也许你也应该这样尝试。首先,检查邮件是否为空白。如果不是空的,检查格式是否正确:
if(empty($_POST['email']))
{
$errors[] = "email cannot be blank";
}else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors[] = "invalid email";
}
和this also:
if(!isset($_POST['date']){
$errors[] = "date is blank";
}else if(!strtotime($_POST['date'])){
$errors[] = "invalid date";
}
首先你的电子邮件代码是说,如果电子邮件不是空的,那么忽略电子邮件的格式检查。我觉得这不是你想要的。此外,您可以使用!filter_var和=== false进行双重否定。
我相信你想要这样的东西:(我把它们分开到不同的行,这样你可以更好地看到流程)
if( empty($_POST['email']) OR
( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false ) ) {
$errors[] = "email is blank or incorrectly formatted";
}
其次,你的约会对象也在做类似的事情,我相信你想再做一次这样的事情:
if( isset($_POST['date']) OR
!strtotime($_POST['date'])) {
$errors[] = "Birth Date must be a valid date";
}
注意:strtotime在PHP 5.1.0之前的版本将返回-1而不是false,您很可能使用的是更高的版本。
编辑:注意我将$email更改为$_POST['email'],因为您从未在您显示的代码中设置过$email
你的逻辑有点不对劲。
对于电子邮件,请输入:
if(empty($_POST['email']))
{
// if no email was entered
$errors[] = "email cannot be blank";
}
else if (filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{
// email was entered, but was invalid
$errors[] = "please enter a valid email";
}
如果$_POST['email']
是空的,如果或不是一个有效的电子邮件地址,你想触发这个if块。因为要检查filter_var()
函数的输出,所以要检查它的是否等于 false
。
日期请输入:
if(!isset($_POST['date']))
{
// if no date was entered
$errors[] = "Birth Date cannot be blank";
}
else if (strtotime($_POST['date']) === false)
{
// date was entered, but was invalid
$errors[] = "please enter a valid date";
}
因为你想触发日期错误,如果a)日期是空的,或者b)它不是一个有效的日期,这个if块应该只在$_POST['date']
不设置时运行,或者如果strtotime()
返回false。