zoukankan      html  css  js  c++  java
  • ajax php 验证注册用户名是否存在

    1.在"test"数据库中,建立一张名为"user"的表.

    sql语句:

    1 create table `user`(
    2 `id` int(5) not null auto_increment primary key,
    3 `username` varchar(10) not null,
    4 `password` varchar(12) not null,  
    5 `email` varchar(50) not null
    6 );

    2.新建register.html文件

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
     3 <head>
     4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
     5     <title>Document</title>
     6     <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"></script>
     7     <script type = "text/javascript"> 
     8         $(document).ready(function(){
     9             $(".register input:first").blur(function(){
    10                 $.ajax({
    11                     type: "post",
    12                     url: "register.php",
    13                     data: "username=" + $(".register input:first").val(),
    14                     success: function(msg) {
    15                         $("#userinfo").html(msg);
    16                     }
    17                 });
    18             });
    19         }); 
    20     </script>
    21     <style type="text/css">
    22         *{margin:0px;padding:0px;}
    23         .register{width:550px;height:250px;padding:25px;margin:200px auto;border:2px solid #7aba5f;}
    24         .display {width:350px;height:60px;float:left;margin-right:20px;}
    25         .reginfo {width:150px;height:60px;float:left;margin-right:20px;color:#999999;font-size:13px;line-height:30px;}
    26         .register input{width:300px;height:30px;border:1px solid #7aba5f;}
    27         .register input.submit{width:100px;height:40px;color:white;font-size:16px;background:#7aba5f;border:none;margin-top:30px;margin-left:150px;}
    28     </style>
    29 </head>
    30 <body>
    31     <div class="register">
    32     <div class="display">
    33     账号:<input type="text" name="username"  />
    34     </div>
    35     <div class="reginfo" id="userinfo">
    36     请输入您的账号.
    37     </div>
    38     <div class="display">
    39     密码:<input type="password" name="password" />
    40     </div>
    41     <div class="reginfo">
    42     请输入您的密码.
    43     </div>
    44     <div class="display">
    45     邮箱:<input type="text" name="email"  />
    46     </div>
    47     <div class="reginfo">
    48     请输入您的邮箱.
    49     </div>
    50     <input type="submit" name="submit" size="30" value="注册" class="submit" />
    51     </div>
    52 </body>
    53 </html>

    3.新建register.php文件

     1 <?php
     2     $username = $_POST['username'];
     3     if (!empty($username)) {
     4     mysql_connect("127.0.0.1", "root", ""); 
     5     mysql_select_db("test");
     6     $sql = "SELECT `username` FROM `user` WHERE `username` = '$username' LIMIT 1";
     7     $re = mysql_query($sql);
     8     while ($row = mysql_fetch_assoc($re)) {
     9     $temp = $row;
    10     }
    11     if (empty($temp)) {
    12     echo "<font color=green style='font-size:16px;'><b>恭喜,可以注册!</b></font>";
    13     } else {
    14     echo "<font color=red style='font-size:16px;'><b>抱歉,无法注册!</b></font>";
    15     }
    16     } else {
    17     echo "<font color=red style='font-size:16px;'><b>请输入您的账号.</b></font>";
    18     }             
    19                     
    20 
    21 ?>
  • 相关阅读:
    微信扫码支付模式一和模式二的区别
    spring boot MongoDB的集成和使用
    Java 8 中的 Streams API 详解
    大并发量的订单的解析
    Springboot项目打成war包,部署到tomcat上,正常启动访问报错404
    最详细的虚拟机安装centos7教程
    nginx限制上传大小和超时时间设置说明/php限制上传大小
    一级域名和二级域名的区别是什么?分别有什么作用?
    快递物流查询接口介绍
    java 使用volatile实现线程数据的共享
  • 原文地址:https://www.cnblogs.com/houmin0036/p/5049101.html
Copyright © 2011-2022 走看看