这是HTML代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
<script type="text/javascript" src="./jquery-1.10.1.min.js"></script>
</head>
<body class="login" >
<div class="mibox">
<div class="" style="display: inline-block;">
<div class="myformbd" style="background-size: 100% 100%;">
<div style="width: 100%;text-align: left;">
<label class="mylable">电话号码</label>
<input class="myinput" id="phone" value="" placeholder="请输入电话"/><br/>
<label class="mylable">密码</label>
<input class="myinput" type="password" id="password" value="" placeholder="请输入密码"/><br/>
<label class="mylable">请再次输入密码</label>
<input class="myinput" type="password" id="andPassword" value="" placeholder="请再次输入密码"/><br/>
<div style="width: 100% ;padding-top: 20px;">
<input class="mybutton" type="submit" id="submit" value="注册"/>
<input class="mybutton" type="submit" id="off" value="取消"/>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
(function(){
$(document).keyup(function(event){
if(event.keyCode ==13){
login();
}
});
$('#submit').bind('click', function(){
login();
});
})();
function login(){
var url = '/register.php';
var phone = $('#phone').val();
var password = $('#password').val();
var andPassword = $('#andPassword').val();
$.ajax({
data: {
phone : phone,
password : password,
andPassword : andPassword,
},
dataType : 'json',
success : function(data){
alert(data.msg);
},
error:function(xhr){
alert(xhr.status);
},
type : 'post',
url : url
});
}
</script>
</body>
</html>
这是PHP代码
<?php
header("Content-type:application/json;charset=utf-8");
$phone = $_POST['phone'];
$password = $_POST['password'];
$andPassword = $_POST['andPassword'];
if(!$phone){
$return = array('status' => 1, 'msg' => '账号不能为空');
echo json_encode($return);exit;
}
if(!$password){
$return = array('status' => 1, 'msg' => '输入密码不能为空');
echo json_encode($return);exit;
}
if(!$andPassword){
$return = array('status' => 1, 'msg' => '再次输入密码不能为空');
echo json_encode($return);exit;
}
if($andPassword != $password){
$return = array('status' => 1, 'msg' => '两次输入密码不同');
echo json_encode($return);exit;
}
if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
$return = array('status' => 1, 'msg' => '电话号码不符合规范');
echo json_encode($return);exit;
}
$phone = clean_xss($phone);
$password = clean_xss($password);
//sql部分,pdo处理
$dbh = new PDO('mysql:host=localhost;dbname=user', 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec('set names utf8');
$sql = "insert into TABLE(username, password) values(:username, :password)";
$sql = $pdo->prepare($sql);
$sql->execute(array(':username' => $username));
$sql->execute(array(':password' => $password));
if($sql->fetch(PDO::FETCH_ASSOC)){
$return = array('status' => 1, 'msg' => '注册成功');
print(json_encode($return));exit;
}else{
$return = array('status' => 0, 'msg' => '注册失败');
print(json_encode($return));exit;
}
//防xss
function clean_xss(&$string, $low = False){
if (! is_array ( $string ))
{
$string = trim ( $string );
$string = strip_tags ( $string );
$string = htmlspecialchars ( $string );
if ($low)
{
return True;
}
$string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string );
$no = '/%0[0-8bcef]/';
$string = preg_replace ( $no, '', $string );
$no = '/%1[0-9a-f]/';
$string = preg_replace ( $no, '', $string );
$no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
$string = preg_replace ( $no, '', $string );
return True;
}
$keys = array_keys ( $string );
foreach ( $keys as $key )
{
clean_xss ( $string [$key] );
}
}
点击结果:报错200
200代码是http请求成功的意思!
索嗨
200不是成功吗。。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。