通过JavaScript和Ajax连接PHP+MySQL实现用户登录注册API接口
//定义HTTP连接对象
var xmlHttp;
//实例化HTTP连接对象
function createXmlHttpRequest() {
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
//发起登录请求
function login() {
createXmlHttpRequest();
var name = document.getElementById("username").value;
var password = document.getElementById("password").value;
if(name == null || name == "") {
innerHtml("请输入用户名");
return;
}
if(password == null || password == "") {
innerHtml("请输入密码");
return;
}
var url = "user.php";
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = handleResult;
xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlHttp.send("action=login&name=" + name + "&psd=" + password);
}
//处理服务器返回的结果/更新页面
function handleResult() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var response = xmlHttp.responseText;
var json = eval('(' + response + ')');
if(json['login_result']) {
alert("登录成功!");
//页面跳转
//window.location.href='MainTab/home.html';
} else {
innerHtml("用户名/密码错误");
}
}
}
//插入提示语
function innerHtml(message) {
document.getElementById("tip").innerHTML = "<span style='font-size:12px; color:red;'>" + message + "</span>";
}
<?php
$mysql_server_name="localhost"; //数据库服务器名称
$mysql_username="root"; // 连接数据库用户名
$mysql_password="326699"; // 连接数据库密码
$mysql_database="hello"; // 数据库的名字
// 连接到数据库
$conn=mysql_connect($mysql_server_name, $mysql_username,
$mysql_password);
if(!$conn) {
echo "数据库连接失败!".mysql_error;
}
mysql_select_db($mysql_database, $conn);
//获取url参数
$action = isset($_POST['action']) ? $_POST['action'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$psd = isset($_POST['psd']) ? $_POST['psd'] : '';
if($action=='login') {
login($name, $psd, true);
} else if($action=='register') {
register($name, $psd);
} else if($action=='modifyPsd') {
modifyPsd($name, $psd);
} else if($action=='showAll') {
showAll();
} else {
$result = array("result"=>"error_request");
$json = json_encode($result);
echo $json;
}
close_conn();
/*用户登录*/
function login($name, $psd, $normal) {
global $conn;
if($conn) {
$result = mysql_query("select name,psd from student");
$success = false;
while($row = mysql_fetch_array($result)) {
if($name == $row['name'] && $psd == $row['psd']) {
$success = true;
}
}
if($normal) {
$login_result = array('login_result'=>$success);
$json = json_encode($login_result);
echo $json;
}
}
return $success;
}
/*用户注册*/
function register($name, $psd) {
$tel = $_POST['tel'];
global $conn;
if($conn) {
//数据库查询
$result = mysql_query("select name from student");
$exist = false;
while($row = mysql_fetch_array($result)) {
if($name == $row['name']) {
//注册失败,用户名已存在;
$exist = true;
$register_result = array("register_result"=>false,"error_code"=>0);
$json = json_encode($register_result);
echo $json;
}
}
//插入数据库
if(!$exist) {
$id = mysql_num_rows($result) + 1;
$success = mysql_query("insert into student values('$id', '$name', '$tel', '$psd')");
if($success) {
//注册成功
$register_result = array("register_result"=>$success);
$json = json_encode($register_result);
echo $json;
} else {
//注册失败,数据库插入错误
$register_result = array("register_result"=>$success,"error_code"=>1);
$json = json_encode($register_result);
echo $json;
}
}
}
}
/*修改登录密码*/
function modifyPsd($name, $psd) {
$newpsd = $_POST['newpsd'];
global $conn;
if($conn) {
//用户登录
$login_result = login($name, $psd, false);
//修改密码
if($login_result) {
$success = mysql_query("update student set psd='$newpsd' where name='$name'");
if($success) {
//修改成功
$modify_result = array("modify_result"=>$success);
$json = json_encode($modify_result);
echo $json;
} else {
//修改失败,数据库错误
$modify_result = array("modify_result"=>$success,"error_code"=>1);
$json = json_encode($modify_result);
echo $json;
}
} else {
//修改失败,登录失败
$modify_result = array("modify_result"=>false,"error_code"=>2);
$json = json_encode($modify_result);
echo $json;
}
}
}
//显示所有用户
function showAll() {
global $conn;
if($conn) {
$result = mysql_query("select * from student");
$success = false;
$array_data = array();
$total = mysql_num_rows($result);
//$data = array("total"=>$total,"datas"=>array(array("data"=>"123","name"=>"zhugeheng"),
// array("data"=>"456","name"=>"zhaodanni")
// ));
while($row = mysql_fetch_array($result)) {
$array_temp = array("name"=>$row['name'], "tel"=>$row['tel']);
array_push($array_data, $array_temp);
}
$data = array("total"=>$total,"datas"=>$array_data, "result"=>true);
$json = json_encode($data);
echo $json;
}
}
//关闭连接
function close_conn() {
global $conn;
mysql_close($conn);
}
+-
?>
用html做一个用户登录注册系统 通过JavaScript和Ajax连接PHP+MySQL实现用户登录注册API接口可是只显示了用户的登陆界面
无现登陆与注册,只显示了html与css的代码效果 求大神指教