php 用jquery实现ajax验证,实现已有用户名不可注册,怎么使用jquery啊?跪谢 jquery 插件文档已经很全了,还有demo.自己要学会钻研啊.记得不错的话.demo中有个ajax列子. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JS:$("#username").blur(function() { var validateUsername = $('#validateUsername'); var t = this; if (this.value != this.lastValue) { if (this.timer) clearTimeout(this.timer); validateUsername.html('用户名检测中...'); this.timer = setTimeout(function () { $.ajax({ type: "POST", url: "reg.php?action=checkusername", data: 'username=' + t.value, success: function(msg){ if(msg==1) validateUsername.html('该用户名已存在'); else validateUsername.html('该用户名可用'); } }); }, 200); this.lastValue = this.value; } });HTML:<tr class="dark-row"> <td width="85" class="require-field">帐户名称</td> <td width="227"><input name="username" type="text" class="text-box" id="username" size="20"/><span id="validateUsername"></span></td> </tr>PHP:reg.php名字检查$username = $_POST['username'];相关数据库操作 if (名字存在) { echo 1; }else echo 0; exit; }} 我只有一个jquery.js,别的都没有啊,没看到demo,方便给我传一个么?谢啦 十分感谢,但是我在reg.php 中的连接数据库检测用户名时,如果只是简单的echo json_encode(array ('username'=>$_REQUEST['username']));的话没问题,但是这个文件已连接数据库就出错,没有反应,这个文件应该怎么写啊?谢啦!!! http://docs.jquery.com/Plugins/Validation自己去看下吧. 呵呵,例子不多,没看到我想要的,我想要看看就像2楼哪样,前台数据传到.php文件时连接数据库做验证时的代码,能给我发过来些例子么?十分感谢!!! 方便给我个完整的reg.php代码么?十分感谢~ 都是最简单的数据库操作不用再给你贴代码了吧?链接数据库出错,那就应该检查你的链接数据库部分是否正确。$dbh = mysql_connect('localhost:3306','帐户','密码'); mysql_select_db('数据库名称');$sql = "select username from 表名 where username='$username'";$ret = mysql_query($sql);$row = mysql_fetch_array($ret); if($row['username']) echo 1;else echo 0; 哦,谢谢,原来我昨天sql语句写错了,忘了个',所以一直没有结果,十分感谢~ 三个页面conn.php GET.php action.php(conn.php POST.php action.php) dreamweaver编译器-----------------conn.php-----------<?php$link=mysql_connect("localhost","root","315824")or die("错误信息:".mysql_error());$db=mysql_select_db("ambow");mysql_query("set names gb2312");?>----------GET.php------------------<!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=gb2312" /><title>AJAX</title></head><script type="text/javascript">var xmlHttp;function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp=new ActiveXObject("Microsoft.XMLHttp"); }else if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }}function GetRequest(txtVal){ createXMLHttpRequest(); xmlHttp.onreadystatechange=Get; xmlHttp.open("GET","action.php?txt="+txtVal,true); xmlHttp.send(null);}function Get(){ if(xmlHttp.readyState==4){ if(xmlHttp.status==200){ document.getElementById("msg").innerHTML=xmlHttp.responseText; } }}</script><body>用户名:<input type="text" name="txt" onBlur="GetRequest(this.value)" /> <span id="msg"></span></body></html>----------action.php----------------<?phprequire_once("conn.php");header("Content-type: text/html; charset=gb2312");$name=$_REQUEST["txt"]; $sql="select * from student where sname='{$name}' ";$result=mysql_query($sql);$row=mysql_fetch_assoc($result);//echo $row["sname"];if($name == NULL){ echo "用户名不能为空";}else if($row["sname"]!=NULL){ echo "该用户名以存在!";}else{ echo "用户名可用";} ?>------------POST.php---------------<!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=gb2312" /><title>AJAX</title></head><script type="text/javascript">var xmlHttp;function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp=new ActiveXObject("Microsoft.XMLHttp"); }else if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }}function PostRequest(txtVal){ createXMLHttpRequest(); xmlHttp.open("POST","action.php",true); xmlHttp.onreadystatechange=Post; xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gb2312"); xmlHttp.send("txt="+txtVal);}function Post(){ if(xmlHttp.readyState==4){ if(xmlHttp.status==200){ document.getElementById("msg").innerHTML=xmlHttp.responseText; } }}</script><body>用户名:<input type="text" name="txt" onBlur="PostRequest(this.value)" /> <span id="msg"></span></body></html> LNMP环境下所有PHP文件的页面全顶部空行,审查元素head内容全在body内 zendframework,出现了连接中断,提示说连接被重置 数据导出到excel的问题 为什么要UNSET? 请问PHP怎么确定单选按钮组有一个被选了? 暑假写了个PHP的网站,主要方向WEB编程,大家帮忙测试一下!谢谢 各位大侠,我觉得PHP的配置好麻烦的,看不懂 高手看看这段代码.ADODB.Recordset (0x800A0E79)错误 在php中画图?在线等 PHP连接oracle数据库问题 PHP6.0 很多函数不再向下兼容了? 如何实现用PHP程序登录163邮箱并取得当前用户的通讯录列表
$("#username").blur(function() {
var validateUsername = $('#validateUsername');
var t = this;
if (this.value != this.lastValue) {
if (this.timer) clearTimeout(this.timer);
validateUsername.html('用户名检测中...');
this.timer = setTimeout(function () {
$.ajax({
type: "POST",
url: "reg.php?action=checkusername",
data: 'username=' + t.value,
success: function(msg){
if(msg==1) validateUsername.html('该用户名已存在');
else validateUsername.html('该用户名可用');
}
});
}, 200);
this.lastValue = this.value;
}
});HTML:
<tr class="dark-row">
<td width="85" class="require-field">帐户名称</td>
<td width="227"><input name="username" type="text" class="text-box" id="username" size="20"/><span id="validateUsername"></span></td>
</tr>PHP:
reg.php
名字检查
$username = $_POST['username'];相关数据库操作 if (名字存在) {
echo 1;
}else
echo 0;
exit;
}
}
echo json_encode(array ('username'=>$_REQUEST['username']));的话没问题,但是这个文件已连接数据库就出错,没有反应,这个文件应该怎么写啊?谢啦!!!
自己去看下吧.
mysql_select_db('数据库名称');
$sql = "select username from 表名 where username='$username'";
$ret = mysql_query($sql);
$row = mysql_fetch_array($ret);
if($row['username'])
echo 1;
else
echo 0;
-----------------conn.php-----------
<?php$link=mysql_connect("localhost","root","315824")
or die("错误信息:".mysql_error());$db=mysql_select_db("ambow");
mysql_query("set names gb2312");?>
----------GET.php------------------<!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=gb2312" />
<title>AJAX</title>
</head>
<script type="text/javascript">
var xmlHttp;function createXMLHttpRequest(){ if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}function GetRequest(txtVal){ createXMLHttpRequest();
xmlHttp.onreadystatechange=Get;
xmlHttp.open("GET","action.php?txt="+txtVal,true);
xmlHttp.send(null);
}function Get(){ if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
document.getElementById("msg").innerHTML=xmlHttp.responseText;
}
}}</script><body>
用户名:<input type="text" name="txt" onBlur="GetRequest(this.value)" />
<span id="msg"></span>
</body>
</html>
----------action.php----------------<?php
require_once("conn.php");header("Content-type: text/html; charset=gb2312");
$name=$_REQUEST["txt"];
$sql="select * from student where sname='{$name}' ";
$result=mysql_query($sql);
$row=mysql_fetch_assoc($result);
//echo $row["sname"];if($name == NULL){
echo "用户名不能为空";
}else if($row["sname"]!=NULL){
echo "该用户名以存在!";
}else{
echo "用户名可用";
}
?>------------POST.php---------------<!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=gb2312" />
<title>AJAX</title>
</head>
<script type="text/javascript">
var xmlHttp;function createXMLHttpRequest(){ if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}function PostRequest(txtVal){ createXMLHttpRequest();
xmlHttp.open("POST","action.php",true);
xmlHttp.onreadystatechange=Post;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gb2312");
xmlHttp.send("txt="+txtVal);
}function Post(){ if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
document.getElementById("msg").innerHTML=xmlHttp.responseText;
}
}}</script><body>
用户名:<input type="text" name="txt" onBlur="PostRequest(this.value)" />
<span id="msg"></span>
</body>
</html>