我正在尝试创建一个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!
}
?>
有什么想法吗?
答案 0 :(得分:0)
你的逻辑只是一个微不足道。
对于电子邮件,请尝试:
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']
为空白,或,如果它不是有效的电子邮件地址,则需要触发此项。由于您正在检查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。
答案 1 :(得分:0)
首先,您的电子邮件代码是说如果电子邮件不是空的,则忽略电子邮件格式检查。我不认为这是你想要的。另外,你有一个双重否定使用!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";
}
注意:在PHP 5.1.0之前的版本中,strtotime将返回-1而不是false,您最有可能使用比您更高的版本。
编辑:请注意,我将$ email更改为$ _POST [&#39; email&#39;],因为您从未在所显示的代码中的任何位置设置$ email。
答案 2 :(得分:0)
你可能也应该尝试这样做。首先,检查电子邮件是否为空白。如果不是空白,请检查其格式是否正确:
if(empty($_POST['email']))
{
$errors[] = "email cannot be blank";
}else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors[] = "invalid email";
}
这也是:
if(!isset($_POST['date']){
$errors[] = "date is blank";
}else if(!strtotime($_POST['date'])){
$errors[] = "invalid date";
}