php+jquery+ajax无刷新验证用户名是否已存在,要一个尽量短的例子。

解决方案 »

  1.   

    今天不忙,陪你<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">
    $(function(){ 
    $("#getjson").bind("click",function(){
    $.ajax({
      type: "POST",
       url: "index.php",
       data: "name="+$("#username").val(),
       success: function(msg){ 
        alert(msg);
       } });
    });
    })
    </script>
    </head>
    <body>
    <input type="text" id="username" /><button id="getjson">获取</button>
    </body>
    </html>
    //index.php
    if(isset($_POST["name"])){
    //写你自己的验证规则
    if($_POST["name"] == "hello")
    echo "success";
    else
    echo "fail";
    }
      

  2.   

    今天不忙,玩玩<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">
    $(function(){ 
    $("#getjson").bind("click",function(){
    $.ajax({
      type: "POST",
       url: "/index.php",
       data: "name="+$("#username").val(),
       success: function(msg){ 
        alert(msg);
       } });
    });
    })
    </script>
    </head>
    <body>
    <input type="text" id="username" /><button id="getjson">获取</button>
    </body>
    </html>//index.php
    if(isset($_POST["name"])){
    //你自己的规则
    if($_POST["name"] == "hello")
    echo "success";
    else
    echo "fail";
    }
      

  3.   

    <script type="text/javascript">
    function check(){$(function(){ 
        $("#getjson").bind("click",function(){
            $.ajax({
                  type: "POST",
                   url: "/index.php",
                   data: "name="+$("#username").val(),
                   success: function(msg){ 
                        alert(msg);                    此处获得返回值后如何在function check()里return false?               }        });
        });
    })}
    </script>