dqyuipw44576 2012-04-28 05:11
浏览 7

我想检查一下用户的存在

Here is the form to have ajax check out user existence.

<!DOCTYPE html>

<html>
    <head><title>Register new user!</title>
    <script src="jquery-1.7.1.min.js"></script>
    </head>
    <body>
        Username:
        <input type="text" name="username" id="username"/><span id="user"></span><br/>
        Password:
        <input type="password" name="password" id="password"/><br/>
        <input type="button" value="Register" name="submit" id="submit" onclick="register_user();"/>
    </body>
    <script>
        function register_user()
        {
            $.ajax(
                {
                    type:"POST",
                    data:username,
                    url:"userexists.php"                    
                })
            .fail(function()
                  {
                    $('#user').html("This user already exists");
                  }
            );                
        }
    </script>
</html>

And here is the userexists.php module

<?php
    // connection to the db
    define(IPHOST,"localhost");
    define(DBPASSWORD,"");
    define(DBUSER,"root");
    define(DATABASE,"ajaxtest");
    define(TABLENAME,"at");

    $conn=mysql_connect(IPHOST,DBUSER,DBPASSWORD) or die(mysql_error());
    mysql_select_db(DATABASE) or die(mysql_error());
    $username=$_POST('username');
    $sql="SELECT username FROM ".TABLENAME." WHERE username=".$username;
    $query=mysql_query($sql);
    if(0!=mysql_numrows($query))
    {
        //
    }
    else
    {

    }
?>

But I am stuck to really figure out how the ajax function actually works, what should I enter the blank field after I know that the entered username has been used, for example ? I don't understand ajax at all.

  • 写回答

1条回答 默认 最新

  • dpka7974 2012-04-28 05:25
    关注

    You need to return a JSON encoded array (look at http://php.net/manual/en/function.json-encode.php). It will essentially return something like this:

    if(0!=mysql_numrows($query))
        {
            echo '{';
            echo '"myMessage":"'.$username." was found'",';
            echo '"myStatus":"1",';
            echo '}';
        }
        else
        {
            echo '{';
            echo '"myMessage":"'.$username." was NOT found'",';
            echo '"myStatus":"0",';
            echo '}';
        }
    

    Then, your AJAX handles it something like this:

        $.ajax({
            type: "GET",
            url: "userexists.php",
            data: dataString,
            dataType: "json",
            success: function(data) {
                if(data.myStatus== 'Y') {
                    var msg = data.myMessage;
                }
            }
        });
    

    (I realize this is a pretty broad answer, but AJAX calls from PHP using JSON results warrants more than a one paragraph answer for full understanding. There are a lot of good examples if you google jquery AJAX GET JSON results )

    The AJAX Success:/Failure: is whether or not the AJAX call was successful, it has nothing to do with with the called page might return. e.g. Failure will fire if the page you call doesn't exist, or if the code crashes. Your script decides what to return, and the AJAX call must be successful to retrieve whatever that page is sending you.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器