我用了bootstrap框架,不知为什么一直表单不能正常排序显示,不过主要学习php逻辑,先不纠结。

登录页:test.php

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Bootstrap 101 Template</title>

<!-- Bootstrap -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="row">
<div class="col-md-12">
<form class="form-horizontal" action="produce_action.php" method="post">
<div class="form-group">
<div class="col-2">
<label for="inputEmail3">用户名</label>
</div>
<div class="col-10">
<input type="text" class="form-control" id="inputEmail3" name="name" placeholder="用户名">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword3" name="pwd" placeholder="密码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">登录</button>
</div>
</div>
</form>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

处理页:
produce_action.php

<?php
header("Content-Type: text/html;charset=utf-8");
$name = $_POST["name"];
$pwd = $_POST["pwd"];
if(is_null($name) || strlen($name) ==0 ){
echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('请输入用户名!');</script>";
}else if(is_null($pwd)||strlen($pwd) == 0){
echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('请输入密码!');</script>";
}else{
echo "name= ".$name;
echo "<br/>pwd=".$pwd;
if(!is_null($name))
$_SESSION['userId']=$name;
echo "<br/> session=".$_SESSION['userId'];
}
?>

当然,最好的是加入数据库操作。数据库操作在前面的blog中已经涉及了,读者可以去看看,把这一块也加上:​

进阶版,增加数据库判断:

<?php
header("Content-Type: text/html;charset=utf-8");
$name = $_POST["name"];
$pwd = $_POST["pwd"];
if(is_null($name) || strlen($name) ==0 ){
echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('请输入用户名!');</script>";
}else if(is_null($pwd)||strlen($pwd) == 0){
echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('请输入密码!');</script>";
}else{
$conn=mysql_connect("localhost", "root","root");
if(!$conn){
echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('连接数据库失败!');</script>";
return;
}

mysql_select_db("test",$conn);
$sql = "select * from users where userName='".$name."'";
$info1=mysql_query($sql,$conn);
if($row = mysql_fetch_object($info1)){
if($row->UserPwd==$pwd){
echo "登录成功!";
echo '<br>';
break;
}else{
echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('密码错误!');</script>";
break;
}
}else{
echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('不存在该用户!');</script>";
}
}
?>

注意,这里的test要替换成数据库名,数据库里面要添加数据方便测试