前台的注册页面
titlehtml,
body {
height: 100%;
}
.row {
position: relative;
box-sizing: border-box;
width: auto;
height: 70px;
margin-left: 32px;
margin-right: 32px;
padding-top: 40px;
border-bottom: 1px solid #888;
}
.input {
width: 100%;
height: 20px;
border: none;
font-size: 20px;
}
.button {
position: relative;
margin-left: 32px;
margin-right: 32px;
margin-top: 32px;
width: auto;
height: 50px;
background-color: #e3007f;
color: #fff;
font-size: 24px;
line-height: 50px;
text-align: center;
border-radius: 8px;
}
.highlight {
opacity: 0.7;
}
apiready = function() {
};
下面是从php接口取出数据并判断。
function fnRegister() {
var uname = $api.byId('userName').value,
pwd = $api.byId('password').value;
phone = $api.byId('phone').value;
api.ajax({
url:'http://192.168.0.117/APIcloud-PHP/PHP/register.php',//如果地址访问不到会请求出错,请填写自己的接口地址
method:'post',
cache:'false',
timeout:5,
dataTpye:'json',
data:{
values:{
uname: uname,
pwd: pwd,
phone: phone
}
}
},function(ret,err){
if (ret.status==1 && ret.num<1) {//各种条件都符合标准的话,注册成功。
alert(ret.msg);
api.execScript({ 将注册成功的用户名传到登陆页面。
name: 'login',
frameName: 'login_frame',
script: 'fnSetUserName("' + ret.uname + '");'
});
api.closeWin();
}else if(ret.num>=1) {// num 是根据你注册的时候输入的用户名,在后台查询有没有同样的用户名,如果从后台传过来的num值大于等于一是用户名已存在。
alert(ret.msg);
}else if(ret.status==2){// status是后台判断用户填写的注册信息的完整性,如果有信息没有填写会提示“用户信息不完整”。
alert(ret.msg);
}
});
};
注册的php接口
header("content-type:text/html;charset=utf-8");
$dsn = "mysql:host=localhost;dbname=apicloud";
$user = "root";
$pwd = "";
$pdo = new PDO($dsn,$user,$pwd,array(PDO::ATTR_PERSISTENT=>TRUE));
$pdo->exec("set names utf8");
$uname = trim($_POST['uname']);
$pwd = md5($_POST['pwd']);
$phone = trim($_POST['phone']);
if($uname==""||$pwd==""||$phone==""){
$msg[] = "用户名信息不完整";
$status[] = "2";
$list['msg'] = $msg;
$list['status'] = $status;
echo json_encode($list);
exit;
}
$data = "select * from uname where uname='$uname'";
$res = $pdo->prepare($data);
//$res->exec();
$res->execute();
$num = $res->rowCount();
if($num>=1){
$msg[] = "用户名已存在,请更换用户名";
$status[] = "1";
$list['msg'] = $msg;
$list['num'] = $num;
$list['status'] = $status;
echo json_encode($list);
exit;
}
$sql = "insert into uname (uname, pwd, phone) values ('$uname', '$pwd', '$phone')";
$result=$pdo->query($sql);
if($result){
$status[] = "1";
$msg[] = "注册成功";
$list['status'] = $status;
$list['msg'] = $msg;
$list['data'] = $result;
$list['uname'] = $uname;
$list['num'] = $num;
echo json_encode($list);
}else{
$status[] = "0";
$msg[] = "注册失败,请重试";
$list['status'] = $status;
$list['msg'] = $msg;
echo json_encode($list);
}
?>