PHP+MYsql+Ajax实现省市区三级联动和用户注册/登录验证.
项目目录
数据库设计
省市区数据库
用户数据库
数据库信息页面“conn.inc.php”
<?php
define("HOST",'localhost');
define("USER",'root');
define("PWD",'Aa123123');
define("DBNAME",'news');
?>
数据库连接页面“mysqli.php”
<?php
include 'conn.inc.php';
$mysqli=new mysqli(HOST,USER,PWD,DBNAME);
if($mysqli->connect_errno){
die('数据库链接出错'.$mysqli->connect_error);
}
?>
账户注册页面“region.php”
<!DOCTYPE html>
<html>
<head>
<title>用户注册</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="../jquery/jquery-1.11.0.js" type="text/javascript"></script>
<script src="../js/showcity.js" type="text/javascript"></script>
</head>
<body>
<form method="post" action="changecity.php">
账户:<input id="account" name="account" type="text" />
密码:<input id="password" name="password" type="password"/> </br>
<sapn id="showcity">我的位置:</span>
<select id="provinces" name="provinces"><option value="">请选择省</option></select>
<select id="citys" name="citys"><option value="">请选择市</option></select>
<select id="countys" name="countys"><option value="">请选择区/县</option></select> </br>
<input type="submit" value="注册"/>
</form>
</body>
</html>
省市区三级联动信息,ajax请求后台获取数据“showcity.js”
// JavaScript Document
$(document).ready(function() {
// 加载所有的省份
$.ajax({
type: "get",
url: "region_action.php", // type=1表示查询省份
data: {"parent_id": "1", "type": "1"},
dataType: "json",
success: function(data) {
$("#provinces").html("<option value=''>请选择省</option>");
$.each(data, function(i, item) {
// alert(item.region_id);
$("#provinces").append("<option value='" + item.region_id + "'>" + item.region_name + "</option>");
});
}
});
$("#provinces").change(function() {
$.ajax({
type: "get",
url: "region_action.php", // type =2表示查询市
data: {"parent_id": $(this).val(), "type": "2"},
dataType: "json",
success: function(data) {
$("#citys").html("<option value=''>请选择市</option>");
$.each(data, function(i, item) {
$("#citys").append("<option value='" + item.region_id + "'>" + item.region_name + "</option>");
});
}
});
});
$("#citys").change(function() {
$.ajax({
type: "get",
url: "region_action.php", // type =2表示查询市
data: {"parent_id": $(this).val(), "type": "3"},
dataType: "json",
success: function(data) {
$("#countys").html("<option value=''>请选择区/县</option>");
$.each(data, function(i, item) {
$("#countys").append("<option value='" + item.region_id + "'>" + item.region_name + "</option>");
});
}
});
});
//显示地址
/*
$("#countys").change(function() {
var value = $("#provinces").find("option:selected").text()
+ $("#citys").find("option:selected").text()
+ $("#countys").find("option:selected").text();
// alert(value);
$("#region").text("选择的位置:"+ value);
});
*/
});
省市区信息查询页面"region_action.php"
<?php
header("Content-Type:text/html;charset=utf-8");
include '../conn/mysqli.php';
$type = isset($_GET["type"]) ? $_GET["type"] : "";
$parent_id = isset($_GET["parent_id"]) ? $_GET["parent_id"] : "";
// 链接数据库
if ($type == "" || $parent_id == "") {
exit(json_encode(array("flag" => false, "msg" => "查询类型错误")));
} else {
// 链接数据库
$sql="select *from global_region where parent_id=$parent_id AND region_type=$type";
$result=$mysqli->query($sql);
if($result->num_rows>0)
{
$arr=[];
while ($row=$result->fetch_assoc())
{
$arr[$row["region_id"]]['region_id']=$row["region_id"];//$arr[1]["title"]=$row["title"]
$arr[$row["region_id"]]['region_name']=$row["region_name"];//$arr[1]["content"]=$arr["content"]
}
}
$provinces_json = json_encode($arr);
exit($provinces_json);
}
?>
账户登录页面"login.php"
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>用户登录</title>
</head>
<body>
<form method="post" action="checklogin.php" >
账户:<input id="account" name="account" type="text"/>
密码:<input id="password" name="password" type="password"/>
<input id="login" name="login" type="submit" value="登录"/>
<input id="reset" name="reset" type="reset" value="重置"/>
<a href="../register/region.php" target="_parent">没有账号,前往注册</a>
</form>
</body>
</html>
账户注册验证页面"changecity.php"
<?php
header("Content-Type:text/html;charset=utf-8");
$a = trim($_POST["provinces"]); //获取选择省份的ID
$b = trim($_POST["citys"]); // 获取选择市的ID
$c = trim($_POST["countys"]); //获取选择区的ID
$d = trim($_POST["account"]); //获取提交的账户
$e = md5(trim($_POST["password"])); //获取提交的密码并用md5算法加密
$f = trim($_POST["password"]); //获取提交的明文密码
include("../conn/mysqli.php"); //引入数据库连接核心文件
//判断省市区提交的ID信息是否为空
if($a=="" && $b=="" && $c==""){
echo "<script>setTimeout(function(){window.location.href='region.php';},1000);</script>";
exit("未选择省市区信息!");
}
//查询省份ID对应的省名称,并以数组方式返回
$provinces = "select region_name from global_region where region_id = $a" ;
$res = $mysqli->query($provinces);
$rows = $res->fetch_assoc();
//查询市ID对应的市名称,并以数组方式返回
$city = "select region_name from global_region where region_id = $b";
$res1 = $mysqli->query($city);
$rows1 = $res1->fetch_assoc();
//查询区ID对应的区名称,并以数组方式返回
$country = "select region_name from global_region where region_id = $c";
$res2 = $mysqli->query($country);
$rows2 = $res2->fetch_assoc();
//赋值查询结果的省市区名称连接起来。
$pcc = $rows["region_name"].$rows1["region_name"].$rows2["region_name"];
var_dump($pcc);
//查询数据库已存在账户与提交的是否重复,是,则false,返回注册页面,否,则true,往下继续执行。
$acc = "select account from user where account = '".$d."'";
$res3 = $mysqli->query($acc);
$rows3 = $res3->fetch_assoc();
if($rows3){
echo "<script>alert('Sorry,此账户已存在!');window.location.href='region.php';</script>";
exit();
}
//判断提交的账户和密码是否都不为空值.是,则true,将数据写进数据库,否,则false,返回注册页面。
if($d!="" && $f!=""){
$register = "insert into user (account,password,address) values ('".$d."','".$e."','".$pcc."')";
$regres = $mysqli->query($register);
if($regres){
echo "<script>setTimeout(function(){window.location.href='../login/index.php';},3000);</script>";
}
}else{
echo "<script>alert('注册失败!');window.location.href='region.php'</script>";
}
/*
$provinces = "select region_name from global_region where region_id = $a" ;
$res = $mysqli->query($provinces);
if($res->num_rows > 0 ){
$rows = $res->fetch_assoc();
echo $rows["region_name"];
}else{
die("未查找匹配到当前数据".mysqli_error());
}
*/
/*
$provinces = "select region_name from global_region where region_id = $a or region_id = $b or region_id = $c " ;
$res = $mysqli->query($provinces);
echo $res1->num_rows;
if($res->num_rows > 0 || $res->num_rows < 4){
echo "选择的地址:";
while($rows = mysqli_fetch_array($res)){
echo($rows["region_name"]);
}
}else{
echo "未查找匹配到当前数据".die(mysqli_error());
}
*/
/*
$provinces1 = "select region_name from global_region where region_id = $a or region_id = $b";
$res1 = $mysqli->query($provinces1);
if($res1->num_rows > 0 || $res1->num_rows < 3){
while($rows1 = mysqli_fetch_array($res1)){
echo($rows1["region_name"]);
}
}
*/
?>
账户登录验证页面"checklogin.php"
<?php
//header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["login"])){
exit("非法操作!");
}
include("../conn/conn_mysql.php");
$account = trim($_POST["account"]);
$password = md5(trim($_POST["password"]));
var_dump($account);
var_dump($password);
if($account && $password){
/*
$link = mysqli_connect('localhost','root','Aa123123');
if(!$link){
echo "数据库连接失败!</br>";
}else{
echo "数据库连接成功";
}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'news');
*/
$sql = "select * from user where account = '$account' and password = '$password'";
$res = mysqli_query($link,$sql);
$result = mysqli_fetch_assoc($res);
$asd = mysqli_num_rows($res);
var_dump($result);
echo "</br>";
echo "</br>";
var_dump($res);
echo "</br>";
echo "</br>";
var_dump($asd);
if($asd){
echo "账户、密码正确!";
echo "<script>window.location.href='index.php';</script>";
}else{
echo "账户或密码错误!";
echo "<script>setTimeout(function(){window.location.href='login.php';},3000);</script>";
}
}else{
echo "账户或密码为空!";
echo "<script>setTimeout(function(){window.location.href='login.php';},3000);</script>";
}
mysqli_close($link);
?>
登录/注册成功后,跳转到的页面"index.php"
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>网站主页</title>
</head>
<body>
<h4>欢迎你,程序员1.</h4>
</body>
</html>
到此,所有数据库设计和代码设计已经展示完毕,接下来,继续查看项目运行效果!!!
浏览器地址栏运行http://localhost:8080/news/login/login.php
点击登录,返回失败!
3秒后,自动返回登录页面,点击“没有账号,前往注册”
尝试直接点击注册按钮,返回失败。
1秒后,再次返回注册页面,查看数据库,无非法数据注入!
尝试仅输入省市区信息,然后点击注册按钮,返回注册失败,数据库查询,非法信息并未写进数据库。
输入账户和省市区信息,返回注册失败,数据库亦无非法信息写入!
输入密码和省市区信息,返回注册失败,数据库没有非法数据写入
同理,输入账户和密码,返回注册失败,数据库没有非法信息写入。
最后,依次输入账户,密码和省市区信息,成功注册,数据库录入数据。
返回注册页面,修改密码和省市区信息,再次点击注册,返回用户已存在,查询数据库,原有账户注册信息并未改变。
最后,所有功能均已演示完毕,省市区联动信息参考PHP中文网作者分享的项目内容,我加以修改和变动。