老师,为什么当用户名为空时,没有出现弹框,而且直接跳转到.php的网页了

来源:4-1 商品列表之数据查询

I编程

2018-04-28 17:36:55

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=utf-8" />

<title>弘润裁床系统</title>

<link rel="stylesheet" href="css/login.css" />

</head>

<div class="warp">

<div class="top">弘润裁床管理系统</div>

<div class="top-a"></div><!--login图-->

<div class="user">

<div class="user-title">

           <p>用户注册 </p>

         </div>

 <form class="login-table" action="register.php" method="post">

          <div class="login-left">

            <label class="username">用户名:</label>

            <input type="text" class="yhmiput" name="username"  placeholder="请输入用户名"><p class="zre">5到15个字符,一个汉字为两个字符</p>

          </div>

          <div class="login-right">

            <label class="passwd">密码:</label>

            <input type="password" class="yhmiput" name="password" placeholder="请输入密码"><p class="zre">5到15个字符,一个汉字为两个字符</p>

          </div>

             <div class="login-btn"> <button type="submit">成功注册</button></div>

        </form> 

  

<script src="js/login.js"></script>

</div>

</html>

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=utf-8" />

<title>弘润裁床系统</title>


</head>

<?php

//表单进行了提交处理

if(!empty($_POST['username'])){

//获取变量

 $username = trim($_POST['username']);

 $password = trim($_POST['password']);

//判断用户名,密码不能为空

if(!$username)

{

echo "<script language=\"javascript\">alert(\"用户名不能为空\");window.location.href=\"register.html \"</script>";exit;

}

if(!$password)

{

echo "<script language=\"javascript\">alert(\"密码不能为空\");window.location.href=\"register.html \"</script>";exit;

 }

//数据库链接

$con=mysql_connect('127.0.0.1','root','root');

if(!$con){

echo mysql_error();exit;

}

mysql_select_db('hr_yh');

mysql_set_charset('utf-8');

//判断用户名是否是否存在

$sql="SELECT COUNT(`id`)as tottal FROM `hr_user` WHERE `username`='{$username}'";

$obj=mysql_query($sql);

$ren=mysql_fetch_assoc($obj);

//验证用户是否存在

if(isset($ren['tottal'])&&$ren['tottal']>0){

echo "<script language=\"javascript\">alert(\"用户名已存在,请重新输入\");window.location.href=\"register.html \"</script>";exit;

exit;

}else{

//密码加密

if(!$password){

return false;

}else{

$password=md5(md5($password).'hr');

}

//插入数据

$sql = "INSERT `hr_user` (username, password, createtime) values('{$username}','{$password}','{$_SERVER['REQUEST_TIME']}')";

$obj=mysql_query($sql);

if($obj){

$userId = mysql_insert_id();

echo "<script language=\"javascript\">alert(\"恭喜您注册成功\");window.location.href=\"register.html \"</script>";exit;


}else{

echo mysql_error();exit;

}

}

}

?>

<body></body>

</html>

http://img.mukewang.com/climg/5ae4408400010a6f18190447.jpg

写回答

1回答

guly

2018-04-28

你好,判断用户名为空时进行跳转的话应该放在

if(!empty($_POST['username']){}

 外面判断,参考代码如下:

http://img.mukewang.com/climg/5ae44446000158dc15990579.jpg

如果解决您的问题请采纳,祝学习愉快!

0

0 学习 · 2569 问题

查看课程