用表单实现用户注册,用php加入了用户不能为空限制后,一进入页面就弹出提示用户名不能为空,如何解决
我用php获取表单内容,并加入了用户名,密码之类不能为空的限制后,一进入页面就弹出提示用户名密码不能为空,如何解决,代码如下:<!DOCTYPEhtmlPUBLIC"-/...
我用php获取表单内容,并加入了用户名,密码之类不能为空的限制后,一进入页面就弹出提示用户名密码不能为空,如何解决,代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>会员注册</title>
<link href="images0236/style.css" rel="stylesheet" type="text/css"/>
</head>
<body class="bodycss">
<?php
$username=$_POST["txt_username"];
$pwd1=$_POST["txt_pwd1"];
$pwd2=$_POST["txt_pwd2"];
//echo $username." ".$pwd1." ".$pwd2;
$a1=substr($username,0,1);
if($a1>='a'&&$a1<='z'||$a1>='A'&&$a1<='Z')
{
}
else
{
echo "<script>alert('用户名不为空且必须以字母开头');</script>";
}
if(strlen($username)<=16)
{
}
else
{
echo "<script>alert('用户名必须是0~16位');</script>";
}
if(is_numeric($pwd1))
{
if(strlen($pwd1)!=6)
{
echo "<script>alert('密码必须为6位');</script>";
}
}
else
{
echo "<script>alert('密码必须是数字');</script>";
}
if($pwd1!=$pwd2)
{
echo "<script>alert('两次输入密码不一致');</script>";
}
?>
<form name="forml" action="" method="post">
<table class="tablecss" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3"class="td_top">会员注册</td>
</tr>
<tr>
<td class="td_center1">用户名</td>
<td class="td_center2"><input type="text" name="txt_username" class="txt1"/></td>
<td class="td_center3"><div class="span1"><img src="images0236/reg1.gif" class="img1"/>用户名/姓名</div></td>
</tr>
<tr>
<td class="td_center1">Email地址</td>
<td class="td_center2"><input type="text" name="txt_email" class="txt1" /></td>
<td class="td_center3"><div class="span1"><img src="images0236/reg1.gif" class="img1"/>请输入邮箱</div></td>
</tr>
<tr>
<td class="td_center1">密码</td>
<td class="td_center2"><input type="password" name="txt_pwd1" class="txt1"/></td>
<td class="td_center3"><div class="span1"><img src="images0236/reg1.gif" class="img1"/></div></td>
</tr>
<tr>
<td class="td_center1">确认密码</td>
<td class="td_center2"><input type="password" name="txt_pwd2" class="txt1"/></td>
<td class="td_center3"><div class="span1"><img src="images0236/reg1.gif" class="img1"/></div></td>
</tr>
<tr>
<td colspan="3" align="center"><textarea rows="4" cols="60">
欢迎阅读会员注册协议
本协议的条款和条件适用于使用本网站的所有会员</textarea></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="reset" name="btn_reset" class="btn_1" value="重置"/>
<input type="submit" name="btn_submit" class="btn_1" value="注册"/><br /></td>
</tr>
<tr>
<td colspan="3" class="td_bottom" ></td>
</tr>
</table>
</form>
</body>
</html> 展开
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>会员注册</title>
<link href="images0236/style.css" rel="stylesheet" type="text/css"/>
</head>
<body class="bodycss">
<?php
$username=$_POST["txt_username"];
$pwd1=$_POST["txt_pwd1"];
$pwd2=$_POST["txt_pwd2"];
//echo $username." ".$pwd1." ".$pwd2;
$a1=substr($username,0,1);
if($a1>='a'&&$a1<='z'||$a1>='A'&&$a1<='Z')
{
}
else
{
echo "<script>alert('用户名不为空且必须以字母开头');</script>";
}
if(strlen($username)<=16)
{
}
else
{
echo "<script>alert('用户名必须是0~16位');</script>";
}
if(is_numeric($pwd1))
{
if(strlen($pwd1)!=6)
{
echo "<script>alert('密码必须为6位');</script>";
}
}
else
{
echo "<script>alert('密码必须是数字');</script>";
}
if($pwd1!=$pwd2)
{
echo "<script>alert('两次输入密码不一致');</script>";
}
?>
<form name="forml" action="" method="post">
<table class="tablecss" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3"class="td_top">会员注册</td>
</tr>
<tr>
<td class="td_center1">用户名</td>
<td class="td_center2"><input type="text" name="txt_username" class="txt1"/></td>
<td class="td_center3"><div class="span1"><img src="images0236/reg1.gif" class="img1"/>用户名/姓名</div></td>
</tr>
<tr>
<td class="td_center1">Email地址</td>
<td class="td_center2"><input type="text" name="txt_email" class="txt1" /></td>
<td class="td_center3"><div class="span1"><img src="images0236/reg1.gif" class="img1"/>请输入邮箱</div></td>
</tr>
<tr>
<td class="td_center1">密码</td>
<td class="td_center2"><input type="password" name="txt_pwd1" class="txt1"/></td>
<td class="td_center3"><div class="span1"><img src="images0236/reg1.gif" class="img1"/></div></td>
</tr>
<tr>
<td class="td_center1">确认密码</td>
<td class="td_center2"><input type="password" name="txt_pwd2" class="txt1"/></td>
<td class="td_center3"><div class="span1"><img src="images0236/reg1.gif" class="img1"/></div></td>
</tr>
<tr>
<td colspan="3" align="center"><textarea rows="4" cols="60">
欢迎阅读会员注册协议
本协议的条款和条件适用于使用本网站的所有会员</textarea></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="reset" name="btn_reset" class="btn_1" value="重置"/>
<input type="submit" name="btn_submit" class="btn_1" value="注册"/><br /></td>
</tr>
<tr>
<td colspan="3" class="td_bottom" ></td>
</tr>
</table>
</form>
</body>
</html> 展开
1个回答
展开全部
你这样写当然啦,每次页面打开都执行了这句话,当然弹出
<form name="forml" action="?act=regst" method="post">
if($_GET["act"]=="regst"){
$username=$_POST["txt_username"];
........
if($pwd1!=$pwd2)
{
echo "<script>alert('两次输入密码不一致');</script>";
}
改成这样就不会了
<form name="forml" action="?act=regst" method="post">
if($_GET["act"]=="regst"){
$username=$_POST["txt_username"];
........
if($pwd1!=$pwd2)
{
echo "<script>alert('两次输入密码不一致');</script>";
}
改成这样就不会了
追问
试了,还是不行,是不是你格式有错啊?我初学者,恕我改不出来
追答
?????????我不是写的很清楚了啊
把action='' 改成 action="?act=regst"
在$username=$_POST["txt_username"]; 上一行增加 if($_GET["act"]=="regst"){
在最后一个“}” 下一行增加个 } 就行啦
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询