我正在尝试创建一个基本的注册脚本。当所有表单都为空时,我得到一条消息,说我没有使用有效的电子邮件地址,但我希望它告诉我没有输入任何数据,如果有任何表单为空,我就这样做。逻辑似乎不太对,可能是个小问题,但我找不到。
homepage.php
<html>
<head>
</head>
<body>
<?php
require 'functions.php';
registration_form();
?>
</body>
</html>
显然也function registration_form() {
echo '<form id="register" action="register.php" method="post">';
echo '<fieldset>';
echo '<legend>Register</legend>';
echo '<input type="hidden" name="submitted" id="submitted" value="1" /></br>';
echo '<label for="email">Email Address:</label></br>';
echo '<input type="text" name="email" id="email" maxlength="100" /> </br>';
echo '<label for="password">Password:</label></br>';
echo '<input type="password" name="password" id="password" maxlength="60" /></br>';
echo '<label for="confirmpassword">Confirm Password:</label></br>';
echo '<input type="password" name="confirmpassword" id="confirmpassword" maxlength="60" /></br>';
echo '<input type="submit" name="Submit" value="Submit">';
echo '</fieldset>';
echo '</form>';
}
function validate_registration($password, $confirmpassword, $email) {
if ((!isset($email)) || (!isset($password)) || (!isset($confirmpassword))) {
registration_form();
echo "Please enter the required information:";
}
elseif ((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($password == $confirmpassword)) {
echo "You have registered with: $email";
}
elseif ((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($password !== $confirmpassword)) {
registration_form();
echo "Your password does not match!";
}
else {
registration_form();
echo "You have not entered a valid email address!";
}
}
register.php
require 'functions.php';
$email = $_POST['email'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
validate_registration($password, $confirmpassword, $email);
您可能想使用
if (!empty([VARIABLE]))
在您的验证检查中,因为这些参数值将在您POST时设置为零长度字符串。因此,它们将不能与!isset
一起工作,因为值是设置的,而不是null
。
我认为你可以使用empty()
函数而不是isset()
if ((empty($email)) || (empty($password)) || (empty($confirmpassword))) {
之所以会出现这种情况,是因为如果字段没有填充任何值,isset()
仍然被设置为空,你可以检查如果你转储你的数组var_dump($_POST);
查看php文档:
只是一个提示,因为我不确定,直到我测试了它。
如果您有一个包含参数但没有值的查询字符串(非甚至是等号),如:
http://path/to/script.php?a
The following script is a good test to determine how a is valued:
<pre> <?php print_r($_GET); if($_GET["a"] === "") echo "a is an
empty string'n"; if($_GET["a"] === false) echo "a is false'n";
if($_GET["a"] === null) echo "a is null'n"; if(isset($_GET["a"]))
echo "a is set'n"; if(!empty($_GET["a"])) echo "a is not empty"; ?>
</pre>
我用script.php?A,它返回:
a是空字符串,a被设置为
所以请注意,没有关联值的参数,即使没有等号,被认为是空字符串("),isset()返回true,并认为它为空,但不为false或null。第一次测试后似乎很明显,但我只是想确定一下。
当然,如果我没有在浏览器查询中包含它,脚本返回Array () a为空
这是表单提交,因此设置了每个变量,但在您的情况下,使用空值。试试empty()
代替!isset()
if(variable)
呢?
在过去,我用它来确定变量是否存在,是否有一个返回true的值。如果不是,则返回false。
我也会检查变量的长度。如果password
和confirmpassword
都是空字符串,那么它们都是相等的。