2016-10-04 62 views
0

我想,当阿贾克斯做是为了返回图像,但我不知道如何在回应中包含一个会话变量,这是我的代码:

$(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">') 
     } 
     }); 
    }); 
}); 

然后,我想展现DIV #new-image

回答

4

可以呼应的是图像文件名中process.php这样内的图像$_SESSION["sess_img"];

<?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(); 

而在你的JavaScript代码:

$(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')); 
     } 
    }); 
    }); 
}); 
+1

你应该更加注意HTML注入。我会更像'$('#new-image')。empty()。append($('').attr('src','uploads/img /'+ image).attr( 'class','img-upload'))' – Phil

+0

@ vher2这很好,问题在于它不会更新新图像,我不得不让F5发布新的任何想法? – GePraxa

+1

这是旧图像'$ _SESSION ['sess_img']'? – vher2