html源码
用户名
密码
登录
(function($, doc) {
$.init({
statusBarBackground: '#f7f7f7'
});
$.plusReady(function() {
document.getElementById("loginBtn").addEventListener("tap", function() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
mui.ajax('http://localhost/ServerJson.php', {
data: {
username: username,
password: password
},
dataType: 'json', //服务器返回json格式数据
type: 'post', //HTTP请求类型
timeout: 10000, //超时时间设置为10秒;
success: function(data) {
var result = eval('(' + data + ')'); //js原生方法解析json字符串为json对象
if(result != 1) {
// 如果密码错误,提示一下信息
mui.alert("用户名或密码错误", "登录错误", "关闭");
//mui.alert(result);
return;
}
mui.openWindow({
url: 'main.html',
id: 'main',
});
},
error: function(xhr, type, errorThrown) {
}
});
});
});
}(mui, document));
php代码
header('Content-type:text/html; Charset=utf8');
header( "Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
$name=$_POST['username'];
$pwd=$_POST['password'];
$con = mysqli_connect('localhost','user','password','dbname');//链接数据库
//mysql_set_charset($link,'utf8'); //设定字符集
if(!$con){
die('error:'.mysql_error());
}
$sql_select="select username,password from pre_common_member where username= ?"; //从数据库查询信息
$stmt=mysqli_prepare($con,$sql_select);
mysqli_stmt_bind_param($stmt,'s',$name);
mysqli_stmt_execute($stmt);
$result=mysqli_stmt_get_result($stmt);
$row=mysqli_fetch_assoc($result);
if($row){
if($row['password']==$pwd){
echo 1;//普通用户
}else{
echo 3;//密码错误
}
}else{
echo 4;//用户不存在
}
?>