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!

  • 写回答

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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机数字电压表电路组成及框图
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line