javascript - MySQL查询表行上的onclick事件

标签 javascript php mysql

我打算在表行上运行查询 onclick 事件。

我在 JavaScript 中创建了一个函数,我可以读取每一行的 id。

现在我希望能够以此 id 作为条件来运行查询。

他们能帮我吗?

明白我的问题吗?

下面我展示了我的代码。

-------------Javascript函数-----------------!

    <script>
function addRowHandlers() {
    var table = document.getElementById("tableId");
    var rows = table.getElementsByTagName("td");
    for (i = 0; i < rows.length; i++) {
        var currentRow = table.rows[i];
        var createClickHandler = 
            function(row) 
            {
                return function() { 
                                        var cell = row.getElementsByTagName("td")[1];
                                        var id = cell.innerHTML;
                                        alert("id:" + id);
                                 };
            };

        currentRow.onclick = createClickHandler(currentRow);
    }
}
window.onload = addRowHandlers();
</script>

-----我打算运行的查询------------

$result= mysql_query("select * from tasks where idTask = id");

while($row = mysql_fetch_array($result))
{
    $image = $row['image'];
    header("Content-type:image/jpeg");
    echo $image;
}

enter image description here

最佳答案

您需要使用 Ajax 将您的 id 发送到单独的 php 文件,然后返回数据。我使用 jQuery 库,因此本示例使用 $.post() - ( http://api.jquery.com/jquery.post/ ) 和 jQuery UI .dialog() - ( http://jqueryui.com/dialog/ ) 弹出类似于 alert() 的结果

JavaScript代码

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
function addRowHandlers() {
    var table = document.getElementById("tableId");
    var rows = table.getElementsByTagName("td");
    for (i = 0; i < rows.length; i++) {
        var currentRow = table.rows[i];
        var createClickHandler = 
            function(row) 
            {
                return function() { 
                                        var cell = row.getElementsByTagName("td")[1];
                                        var id = cell.innerHTML;

                                        //use ajax to post id to ajax.php file
                                        $.post( "ajax.php", { id:id }, function( data ) { 
                                              //add result to a div and create a modal/dialog popup similar to alert()
                                              $('<div />').html('data').dialog();
                                        });

                                 };
            };

        currentRow.onclick = createClickHandler(currentRow);
    }
}
window.onload = addRowHandlers();
</script>

ajax.php(单独文件,随机命名为ajax.php)

$id = mysql_real_escape_string($_POST['id');    

$result= mysql_query("select * from tasks where idTask = $id");

while($row = mysql_fetch_array($result))
{
    $image = $row['image'];
    header("Content-type:image/jpeg");
    echo $image;
}

关于javascript - MySQL查询表行上的onclick事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26102461/

相关文章:

php - 获取错误 : Can't connect to MySQL server on 'SERVER_IP' (99)

php - httpd 不会以为 php 添加的行开始

php - PHP 与应用程序之间的通信

javascript - RequireJS 单个文件中的多个模块

javascript - 将 props 传递到 React 组件

javascript - 使对象数组成为Node.js中数组中对象的值

mySQL 如何通过不同表中的最大和查找名称

mysql - 使用 apache 网络服务器从 perl 连接到 mysql?

mysql - 将两个表与第三个表连接起来

javascript - 在支持触摸的浏览器上创建和触发触摸事件?