ajax 实现密码登录

HTML代码

<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="js/jquery.js"></script> //导入js包

</head>
<script>
$(document).ready(function(){
$("#but").click(function(){
var a=$("#name").val();
var b=$("#mima").val();


$.post("i-ajax.php",{'name':a,'mima':b,},function(data){
alert(data); //弹窗
window.location.href="ajaxdonglu.php";
}); //index1.php引入php文件运行这个文件 {}中间写上面var a=$("#name").val();对应的值 funtion定义个变量用alert弹窗输出

});

});

</script>


<body>


<input type="text" id="name" placeholder="姓名" >
<input type="password" id="mima" placeholder="密码至少6位数"><p></p>

<button type="button" id="but">登录</button>

</body>
</html>

PHP代码

<?php


header( 'Content-Type:text/html;charset=utf-8 ');


include_once("conn/conn.php"); //引入导入数据库控制页面

$name=$_POST["name"];
$mima=$_POST["mima"];


$sql="SELECT*from user where name ='$name' and mima='$mima' ";
$r=mysqli_query($link,$sql);// $link要使用的mysql连接 $sql要查询的字符串
$n=mysqli_num_rows($r);
if ($n>0) {
echo "登录成功";
}else{
echo"登录失败";
}

?>

引入数据库的PHP代码

<?php
$host="localhost";//规定主机名和ip地址
$db_user="root"; //数据库用户
$db_pass=""; //数据库密码
$db_name="sqlclass1"; //要使用的数据库数据库
$timezone = "Asia/Shanghai";//上海时间

$link=mysqli_connect($host,$db_user,$db_pass,$db_name);//mysqli_connect打开一个到 MySQL 服务器的新的连接
mysqli_query($link,"SET names UTF8");//link 是规定要使用的mysql连接 后面是设置编码格式
header("Content-Type: text/html; charset=utf-8");
date_default_timezone_set($timezone); //北京时间
?>

原文地址:https://www.cnblogs.com/xinxueyou/p/7884680.html