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

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器