欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php ajax 局部刷新注册验证的例子

程序员文章站 2022-05-29 23:25:39
...
  1. // JavaScript Document

  2. var xmlHttp;
  3. function S_xmlhttprequest()
  4. {
  5. xmlHttp=null;
  6. if (window.XMLHttpRequest)
  7. {// code for IE7, Firefox, Opera, etc.
  8. xmlHttp=new XMLHttpRequest();
  9. }
  10. else if (window.ActiveXObject)
  11. {// code for IE6, IE5
  12. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  13. }
  14. }
  15. function getName(name)

  16. {
  17. if(name = document.myform.name.value)

  18. {
  19. S_xmlhttprequest();
  20. xmlHttp.open("get","data.php?name="+name,true);
  21. xmlHttp.onreadystatechange = byname;
  22. xmlHttp.send(null);
  23. }
  24. }

  25. function byname()

  26. {
  27. if(xmlHttp.readyState ==1)
  28. {
  29. document.getElementById('name').innerHTML = "loading....";
  30. }
  31. if(xmlHttp.readyState == 4)
  32. {
  33. if(xmlHttp.status == 200)
  34. {
  35. var name = xmlHttp.responseText;
  36. document.getElementById('name').innerHTML = name;
  37. }
  38. }
  39. }
  40. function getEmail(email)
  41. {
  42. var email = document.myform.email.value;
  43. if(email =="")
  44. {
  45. alert("用户名不能为空");
  46. document.myform.email.focus();
  47. return false;
  48. }
  49. else
  50. {
  51. S_xmlhttprequest();
  52. xmlHttp.open("get","data.php?email="+email,true);
  53. xmlHttp.onreadystatechange = byemail;
  54. xmlHttp.send(null);
  55. }
  56. }

  57. function byemail()
  58. {
  59. if(xmlHttp.readyState ==1)
  60. {
  61. document.getElementById('email').innerHTML = "loading....";
  62. }
  63. if(xmlHttp.readyState == 4)
  64. {
  65. if(xmlHttp.status == 200)
  66. {
  67. var email = xmlHttp.responseText;
  68. document.getElementById('email').innerHTML = email;
  69. }
  70. }
  71. }
复制代码

2、register.php文件

  1. 注册页面_bbs.it-home.org
  2. 用户名:
    *用户名必填*
    邮箱:
    *邮箱必填*
复制代码

3、data.php页面,用于ajax查询数据库。

  1. sleep(1);
  2. $connt = mysql_connect("localhost","root","123456");
  3. mysql_select_db('test',$connt );
  4. mysql_query("set names 'gb2312'");
  5. if($_GET['name'])
  6. {
  7. $name = $_GET['name'];
  8. $sql = "select * from test where name='$name'";
  9. $restul = mysql_query($sql);
  10. $array = mysql_fetch_row($restul);
  11. // print_r($array);
  12. if(is_array($array))
  13. {
  14. echo "该用户名已经存在";
  15. }
  16. else
  17. {
  18. echo "该用户名可以用";
  19. }
  20. }
  21. if($_GET['email'])
  22. {
  23. $name = $_GET['email'];
  24. $sql = "select * from test where email='$email'";
  25. $restul = mysql_query($sql);
  26. $array = mysql_fetch_row($restul);
  27. // print_r($array);
  28. if(is_array($array))
  29. {
  30. echo "该邮箱已存在";
  31. }
  32. else
  33. {
  34. echo "该邮箱可以用";
  35. }
  36. }
  37. ?>
复制代码

由此即可实现用户注册时局部刷新的功能了。