我正在尝试创建一个基本的注册脚本。当所有表格都留空时,我收到一条消息,说我没有使用有效的电子邮件地址,但我想告诉我,我没有输入任何数据,如果任何表格留空,请执行此操作。逻辑似乎不正确,它可能是一个小问题,但我找不到它。
homepage.php
<html>
<head>
</head>
<body>
<?php
require 'functions.php';
registration_form();
?>
</body>
</html>
的functions.php
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);
答案 0 :(得分:1)
您可能想要使用
if (!empty([VARIABLE]))
在验证检查中,因为这些参数值将在POST时设置为零长度字符串。因此,它们将无法与!isset
一起使用,因为值已设置且不是null
。
答案 1 :(得分:0)
我认为您可以使用empty()
功能而不是isset()
if ((empty($email)) || (empty($password)) || (empty($confirmpassword))) {
发生这种情况是因为如果字段未填充任何值,isset()
仍设置为空,您可以检查是否转储后发布数组var_dump($_POST);
答案 2 :(得分:0)
查看php docs:
请注意,因为在我测试之前我不确定。
如果您的查询字符串包含参数但没有值(不是 甚至是等号),如:
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或 空值。第一次测试后似乎很明显,但我必须确保。
当然,如果我不在浏览器查询中包含它,那么脚本 返回Array()a为null
答案 3 :(得分:0)
这是表单提交因此设置了每个变量,但在您的情况下,设置为空值。请尝试使用empty()
代替!isset()
。
答案 4 :(得分:0)
if(variable)
怎么样?
在过去,我用它来确定变量是否存在并且具有返回true的值。如果没有,它将返回false。
我还会检查变量的长度。如果password
和confirmpassword
都是空字符串,那么它们都是相等的。