2016-10-04 31 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