2016-04-24 41 views
-1

我已經從數據庫中加載了下面的頁面(processor.php)表。當我點擊鏈接時,它會連接到solve.php下載或通過GET和特定Id查看。 當我點擊「下載」,沒有任何問題,圖像下載,而在processor.php留php jQuery ajax調用來查看數據庫中的圖像

  • 問題:當我點擊「查看」單獨的頁面上,頁面顯示。
  • 要求:我想要在同一頁面上顯示圖像預覽。
  • 我的想法:使用jQuery Ajax函數。
  • 我的問題:我應該刪除一個href。在processor.php中添加標籤,而是應該在Ajax函數中使用哪個標籤?

在Ajax函數中,我提到了「?」標記不知道如何處理數據。請完成它?

$(document).ready(function() { 
    $("#?????????").click(function(e) { 
     e.preventDefault(); 

     $.ajax({ 
      url: "solve.php", 
      type: "GET", 
      data: ?????, 

      success: function(data){ //function to be called if request succeeds 
       $("#preview").html(data); 
      } 
     }); 
    }); 
}); 

enter image description here

CREATE TABLE Upload(
Id INT NOT NULL AUTO_INCREMENT, 
Name VARCHAR(30) NOT NULL, 
Type VARCHAR(30) NOT NULL, 
SizeMB DECIMAL(9,2) NOT NULL, 
Content LONGBLOB NOT NULL, 
PRIMARY KEY(Id) 
); 

processor.php

$query = "SELECT Id,Name,Type,SizeMB FROM Upload"; // Good practice. Do not process much data here. leave Content field 
$result = mysqli_query ($con, $query) or die ("Couldn't execute SELECT query: ". mysqli_error($con)); 
$nrows = mysqli_num_rows($result); 
echo '<table> 
         <tr> 
          <td>Id</td> 
          <td>Name</td> 
          <td>Type</td> 
          <td>Size (MB)</td> 
         </tr>'; 
$selfpg = $_SERVER['PHP_SELF']; 
while($row = mysqli_fetch_assoc($result)){ 
    extract($row); 
    echo "<tr> 
          <td>$Id</td> 
          <td>$Name</td> 
          <td>$Type</td> 
          <td>$SizeMB</td> 
          <td> <a href='solve.php?id=$Id'> Download </a> </td> 
          <td> <a href='solve.php?id=$Id&view=preview'> View </a> </td> 
         </tr>"; 
} 
echo '</table>'; 

程序與SQL腳本@BeS代碼

http://s000.tinyupload.com/index.php?file_id=51687103902659541890

回答

1

在你的HTML類添加到鏈接

<a class="image-view" href='solve.php?id=$Id&view=preview'> 

在JavaScript中,找到包含該類的所有元素,並獲得一個點擊的href屬性。

$(".image-view").click(function(e) { 
    e.preventDefault(); 

    var url = $(this).attr('href'); // Get href attribute of current element 
    $.ajax({ 
     url: url, // Use url with arguments 
     type: "GET", 

     success: function(data){ //function to be called if request succeeds 
      $("#preview").html(data); // It should be html here 
     } 
    }); 
}); 
+0

,先生,我測試了你的解決方案@Bes。但它不起作用。我可以上傳文件以便檢查一次嗎?謝謝 – Amali

+1

好吧,它解決了帶有問號的部分。但是,在成功功能中,你應該做正確的事情。 '#preview'元素沒有給出,也沒有'solve.php' ... – BeS

+0

先生,我把鏈接放在我的帖子上。看一下。請說明我所做的錯誤。謝謝 – Amali