douzhongjian0752 2016-10-04 05:49
浏览 12
已采纳

在ajax成功函数中包含$ variable

I am trying to return an image when Ajax is done, but i don't know how to include a session variable in the response, this is my code:

$(document).ready(function(){
    $("#process").click(function(){

      $.ajax({
         type: 'POST',
         url: 'process.php',
         success: function (msg) {
          $('#new-image').html('<img src="uploads/img/$_SESSION["sess_img"];" class="img-upload" alt="new image">')
         }
      });
   });
});

Then, I want show the image $_SESSION["sess_img"]; within the div #new-image

  • 写回答

1条回答 默认 最新

  • doutun9179 2016-10-04 05:57
    关注

    You can echo that image filename in process.php like this:

    <?php
    // using time() as the cache buster, the image will never be cache by the browser
    // to solve it, you need to add condition that will check if the image has change or not.
    // or maybe you need to change the filename if it changes without adding a cache buster.
    echo $_SESSION['sess_img_chofer']."?v=".time();
    

    And in your javascript code:

    $(document).ready(function(){
      $("#process").click(function(){
    
        $.ajax({
          type: 'POST',
          url: 'process.php',
          success: function (image) {
            $('#new-image').html($('<img>').attr('src', 'uploads/img/' + image).attr('class', 'img-upload'));
          }
        }); 
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路