<?php
echo "<h1>Register</h1>";
$submit = isset($_POST['submit']);
$fullname = strip_tags(isset($_POST['fullname']));
$username = strip_tags(isset($_POST['username']));
$password = strip_tags(isset($_POST['password']));
$repeatpassword = strip_tags(isset($_POST['repeatpassword']));
$date = date("Y-m-d");
if ($submit)
{
if ($fullname&&$username&&$password&&$repeatpassword)
{
}
else
echo "Please fill in <b>all</b> fields! ";
}
?>
上面的PHP代码是带有isset函数的已编辑代码。
为什么单击注册按钮后没有出现echo "Please fill in <b>all</b> fields! ";
?有没有成功运行程序的建议?
在插入isset()
函数之前,代码运行成功。谁能解释一下?
任何建议或任何代码结构?
我正在尝试为办公室创建一个登录注册系统。
我是Php的新手。 代码中包含使用的完整html。
谢谢
下面的PHP代码是输出错误的原始代码:
(!)注意:未定义的索引:在第4行的C:\ wamp \ www \ registerloginsystem \ register.php中提交
(!)注意:未定义的索引:第6行的C:\ wamp \ www \ registerloginsystem \ register.php中的fullname
注意:未定义的索引:第7行的C:\ wamp \ www \ registerloginsystem \ register.php中的用户名
注意:第9行的C:\ wamp \ www \ registerloginsystem \ register.php中的未定义索引:密码
注意:第10行的C:\ wamp \ www \ registerloginsystem \ register.php中的未定义索引:repeatpassword
<?php
echo "<h1>Register</h1>";
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = date("Y-m-d");
if ($submit)
{
//check for existence
if ($fullname&&$username&&$password&&$repeatpassword)
{
# code...
// encrypt password
$password = md5($password);
$repeatpassword = md5($repeatpassword);
if ($password==$repeatpassword)
{
# code...
// Check char length of username and fullname
if (strlen($username)>25||strlen($fullname)>25)
{
echo "Length of username or fullname is too long!";
}
else
//check password length
{
if (strlen($password)>25||strlen($password)<6)
{
echo "Password must be between 6 and 25 characters";
}
else
{
// register the user!
}
}
}
else
echo "Your passwords do not match!";
}
else
{
echo "Please fill in <b>all</b> fields! ";
}
}
?>
<!DOCTYPE html>
<html>
<body>
<p>
<form action='register.php' method="POST">
<table>
<tr>
<td>
Your full name:
</td>
<td>
<input type="text" name="fullname">
</td>
</tr>
<tr>
<td>
Choose a username:
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>
Choose a password:
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
Repeat your password:
</td>
<td>
<input type="password" name="repeatpassword">
</td>
</tr>
</table>
<p>
<input type="submit" name="submit" value="Register" >
</form>
</body>
</html>
答案 0 :(得分:1)
您的代码结构不合理。我建议这样的事情:
<?php
$submit = isset($_POST['submit']);
if($submit)
{
if(empty($_POST['fullname']) || empty($_POST['username']) || empty($_POST['password']) || empty($_POST['repeatpassword']))
{
echo "Please fill in all fields!";
}else{
/** continue
* with
* registration
**/
//like strip tags in input e.t.c
}
}
?>
答案 1 :(得分:1)
尝试使用empty()
,如下所示:
<?php
echo "<h1>Register</h1>";
if(!empty($_POST)){
$submit = isset($_POST['submit']);
$fullname = strip_tags(isset($_POST['fullname']));
$username = strip_tags(isset($_POST['username']));
$password = strip_tags(isset($_POST['password']));
$repeatpassword = strip_tags(isset($_POST['repeatpassword']));
}
$date = date("Y-m-d");
if ($submit)
{
if (!empty($fullname) && !empty($username) && !empty($password) && !empty($repeatpassword))
{
}
else
echo "Please fill in <b>all</b> fields! ";
}
?>
<?php
echo "<h1>Register</h1>";
if(!empty($_POST)){
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
}
$date = date("Y-m-d");
if ($submit)
{
//check for existence
if (!empty($fullname) && !empty($username) && !empty($password) && !empty($repeatpassword))
{
# code...
// encrypt password
$password = md5($password);
$repeatpassword = md5($repeatpassword);
if ($password==$repeatpassword)
{
# code...
// Check char length of username and fullname
if (strlen($username)>25||strlen($fullname)>25)
{
echo "Length of username or fullname is too long!";
}
else
//check password length
{
if (strlen($password)>25||strlen($password)<6)
{
echo "Password must be between 6 and 25 characters";
}
else
{
// register the user!
}
}
}
else
echo "Your passwords do not match!";
}
else
{
echo "Please fill in <b>all</b> fields! ";
}
}
?>
答案 2 :(得分:0)
这是您应该检查表单是否已提交的方式。
if($_SERVER['REQUEST_METHOD'] == 'POST') {
//form handling code
}
如果您使用的是PHP 7及以上版本,则可以检查这样的变量
$fullName = $_POST['fullname']?? null;