Home  >  Article  >  php教程  >  使用php+Ajax实现唯一校验实现代码[简单应用]

使用php+Ajax实现唯一校验实现代码[简单应用]

WBOY
WBOYOriginal
2016-06-06 20:39:34829browse

使用php+Ajax实现唯一校验实现代码[简单应用],前台用ajax后台用php,需要的朋友可以参考下。

首先创建一个Ajax类()

然后新建一个文件form.html

--------------------------form.html----------------------------
代码如下:




username:


password:




最后新建一个php文件form.php
--------------------------form.html----------------------------
代码如下:
header("Content-type:text/html;charset=gb2312"); //设置字符集
$mysqli = new mysqli("localhost","root","123","demo"); //打开demo数据库
$result = $mysqli->query("select * from zhanghao where name='{$_POST["username"]}'");
if($result->num_rows > 0){ //判断是否查询出数据
echo "用户{$_POST["username"]}已经存在!";
}else{
echo "用户{$_POST["username"]}可以注册";
}
?>

在浏览器中打开form.html出现下图:

注意:需要提前在MySQL数据库中建立一个表“zhanghao”

如果输入已存在的name,则出现如下提示:

如果输入未存在的name,出现如下提示:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn