dpicx06888
2015-06-29 00:26
浏览 30

使用PHP和Ajax的自定义登录表单检查用户名并显示个人资料图片

i'm trying to do something "different" with my login page, but i think i'm doing somthing wrong or not understanding the process.

Right now, my users table are like:

user_id, username, password, profile_picture, first_name, last_name

What i want is, when you click in the input text (on focus) you start typing your username, once you click out, the ajax will check this user and return at the top the profile picture for that account.

Here it's an example for this but using gravatars: Example Here

Can someone help me to understand how this request can be done with ajax and php?

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我正在尝试与我的登录页面做一些“不同”的事情,但我认为我做错了 或不了解该过程。

现在,我的用户表如下:

user_id,username,password,profile_picture,first_name,last_name

我想要的是,当您点击输入文本(焦点上)时,您开始输入您的用户名,一旦您点击,ajax将检查该用户并返回顶部的个人资料图片 对于那个帐号。

这是一个例子,但是使用了gravatars:示例在这里

有人可以帮我理解如何使用ajax和php完成此请求吗?

谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyeke2695 2015-06-29 01:00
    已采纳

    JQUERY:

    $(function(){
        $("#myInput").blur(function(){
            // On exit, Get Image data
            $.get("getUserImage.php", { user: $(this).val() }, function(data){
                if(data != "error"){
                    $("#userImage").attr("src", data);
                }
            });
        });
    });
    

    PHP (getUserImage.php):

    <?php
    $user = isset($_GET['user'])?$_GET['user']:"";
    
    if(empty($user)){
        die("error");
    }
    
    // connect to MySQL DB w/ MySQLi
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    if ($stmt = $mysqli->prepare("SELECT userImage FROM table WHERE user=?")) {
        // bind parameters
        $stmt->bind_param("s", $user);
        // execute query 
        $stmt->execute();
        // bind result variables
        $stmt->bind_result($image);
        // fetch value
        $stmt->fetch();
        if(!empty($image)){
            printf("http://www.example.com/%s", $image);
        } else {
            echo "error";
        }
        // close statement
        $stmt->close();
    }
    // close connection
    $mysqli->close();
    ?>
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题