php jQuery ajax调用从数据库中查看图像

时间:2016-04-24 09:28:54

标签: php ajax

我从数据库加载了以下页面(processor.php)表。当我点击链接时,它会连接到solve.php,以便通过GET和特定ID下载或查看。 当我点击"下载",没有问题,图片下载,同时留在processor.php

  • 问题:当我点击"查看"时,页面会显示在单独的页面上。
  • 要求:我想在同一页面上显示图像预览。
  • 我的想法:使用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 个答案:

答案 0 :(得分: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
        }
    });
});