doyp9057 2015-02-19 08:16
浏览 22
已采纳

图像不通过AJAX PHP更新

Following code is from a login page. I want to update the image of user when user fill username onkeyup. Image path is to get through PHP. Script code is given below:

function showUserPic(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

//              document.getElementById("img").innerHTML=xmlhttp.responseText;
                x = document.getElementById("img");
                x.src = 'users/'+xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
    }
}

HTML Code is given below:

<input type="text" class="inputLabel1" style="margin:5px;" name="userText" onkeyup="showUserPic(this.value)" />

PHP file code is given below:

<!DOCTYPE html>
<html>
  <head>
  </head>

  <body>
    <?php
      $user = intval($_GET['q']);
      $conn = mysqli_connect("localhost","root","","pharmacy");

      if (!$conn) {
        die('Could not connect: ' . mysqli_error($con));
      }

      $query = "SELECT * FROM users WHERE username = '$user';";
      $result = mysqli_query($conn, $query);
      $userdata = mysqli_fetch_array($result);

      mysqli_close($conn);

      echo $userdata['image'];
    ?>
  </body>
</html>
  • 写回答

4条回答 默认 最新

  • duanliang789262 2015-02-19 08:37
    关注

    As @dan-klasson stated - you need to return from server only image path. Your HTML and JS can stay AS-IS, but you need to change your PHP markup to just:

    <?php
      $user = intval($_GET['q']);
    
      $conn = mysqli_connect("localhost","root","","pharmacy");
      if (!$conn) {
          die('Could not connect: ' . mysqli_error($con));
      }
    
      $query = "SELECT * FROM users WHERE username = '$user';";
      $result = mysqli_query($conn, $query);
      $userdata = mysqli_fetch_array($result);
      mysqli_close($conn);
    
      echo $userdata['image'];
    ?>
    

    Your actual code are returning:

    <!DOCTYPE html>
    <html>
      <head>
      </head>
    
      <body>
        example_image.jpg
      </body>
    </html>
    

    and your script are inserting all of this into your image tag, so it will be:

    <img src="users/%3C!DOCTYPE%20html%3E%0A%20%20%20%20%3Chtml%3E%0A%20%20%20%20%3Chead%3E%0A%20%20%20%20%3C%2Fhead%3E%0A%20%20%20%20%3Cbody%3E%0A%20%20%20%20example_image.jpg%0A%20%20%20%20%3C%2Fbody%3E%0A%20%20%20%20%3C%2Fhtml%3E"/>
    

    It's probably not what you want :) Just change your PHP code.

    EDIT: One additional mistake in your code is that you have intval on your request parameter (which tries to be username):

    $user = intval($_GET['q']);
    

    Change it to:

    $user = $_GET['q'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭