其实道理挺简单的,前期是一直不懂,和语法的生疏导致的。
大体的代码如下:
Html代码
<form action="#" method="post" onsubmit="return check();"> //onsubmit事件 当点击了提交按钮触发事件。
<input type="text" name="test" id="test"/>
<span id="intxt"></span>
<input type="submit" />
</form>
jQuery代码
function check() //创建按钮方法
{
var a = $("#test").val(); //将获取到表单的值,赋值给变量a
a == '' ? alert("1") : ''; //如果a等于空 弹出1,表示错误
$.ajax({
cache:false, //关闭缓存
url: 's.php/?user='+ a, //提交的连接 + 要提交的值(变量)。
type: 'post', //提交的方式
dataType: 'json', //数据的格式
success:function( data ){
a == '' ? console.log("您的输入有误") : console.log("您提交的是:"+a) ;
if(data.status == a) //status 在php里面做判断用
{
alert(data.msg);
}
else
{
alert(data.msg);
}
}
});
}
PHP代码
<?php
include('db.class.php');
$db = new db('127.0.0.1', 'root', 'xxx', 'xxx');
$user = $_GET['user'];
$row = $db->get("SELECT * FROM rusers WHERE username='{$user}'");
if ($row['username'] != $user)
{
echo json_encode(array('status' => '200', 'msg' => "可以注册"));
}
else
{
echo json_encode(array('status' => '0', 'msg' => "用户已经存在"));
}
Tags: None
Archives QR Code