温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php如何应用Ajax技术检测用户名

发布时间:2021-10-19 10:19:19 来源:亿速云 阅读:117 作者:小新 栏目:web开发

这篇文章主要介绍php如何应用Ajax技术检测用户名,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1.搭建Ajax开发框架,代码如下

<script language="javascript"> var http_request = false; function createRequest(url) {     //初始化对象并发出XMLHttpRequest请求     http_request = false;     if (window.XMLHttpRequest) {                                        //Mozilla等其他浏览器         http_request = new XMLHttpRequest();         if (http_request.overrideMimeType) {             http_request.overrideMimeType("text/xml");         }     } else if (window.ActiveXObject) {                              //IE浏览器         try {             http_request = new ActiveXObject("Msxml2.XMLHTTP");         } catch (e) {             try {                 http_request = new ActiveXObject("Microsoft.XMLHTTP");            } catch (e) {}         }     }     if (!http_request) {         alert("不能创建XMLHTTP实例!");         return false;     }     http_request.onreadystatechange = alertContents;                     //指定响应方法                                                                                                    http_request.open("GET", url, true);                                 //发出HTTP请求     http_request.send(null); } function alertContents() {                                               //处理服务器返回的信息     if (http_request.readyState == 4) {         if (http_request.status == 200) {             alert(http_request.responseText);         } else {             alert('您请求的页面发现错误');         }     } } </script>

2.编写JavaScript的自定义函数checkname();用于检测用户名是否为空,当用户名不为空时,调用createRequest() 方法发送请求检测用户名是否存在,代码如下:

function checkname() {     var username = form1.user.value;     if (username=="")     {         window.alert ("请输入用户名");            }     else     {                                                                               createRequest('action/checkname.php?username='+username+'&nocache='+new Date().getTime());     } }

3.在页面添加"检测用户名超链接"

<td width="93" class="ziti2"><a href="#" onclick="checkname()">[检测用户名]</a></td>

4.创建checkname.php处理页面,代码如下

<?php header("Content-type= text/html; charset=utf8"); include "../conn/conn.php"; $GB2312string=iconv( 'UTF-8', 'gb2312//IGNORE' , $RequestAjaxString);           //Ajax中先用encodeURIComponent对要提交的中文进行编码 $username = $_GET[username]; $sql=mysql_query("select * from user where username='".$username."'")or die (mysql_error()); $info=mysql_fetch_array($sql); if($info) {     echo "祝贺您!用户名[".$username."]没有被注册!"; } else {     echo "祝贺您!用户名[".$username."]没有被注册!"; } ?>

以上是“php如何应用Ajax技术检测用户名”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI