【问题标题】:When clicking HTML button, how do I identify what table row it is being clicked from using PHP? [duplicate]单击 HTML 按钮时,如何使用 PHP 识别正在单击的表格行? [复制]
【发布时间】:2021-07-26 04:29:21
【问题描述】:

我需要知道如何使用 php.ini 找出每个按钮被点击的那一行。这可能吗?

<?php
    if (isset($_POST['btnDisplay'])) {

        $sql="SELECT * FROM tbl_Item";

        $result = $conn->query($sql);

        $numRows = mysqli_num_rows($result);

        print "</br>";
        print "</br>";
        print "</br>";
        print "</br>";

        print '<table style="width:100%">';
        print "<tr>";
            print "<th>ItemID</th>";
            print "<th>Description</th>";
            print "<th>Cost_Price</th>";
            print "<th>Quantity</th>";
            print "<th>Sell_Price</th>";
            print "<th>Image</th>";
            print "<th>Add to cart</th>";
        print "</tr>";

        $costs = array();
            
        for ($i=0; $i < $numRows; $i++) { 

            $sqlSelect =   "SELECT * 
                            FROM tbl_User
                            LIMIT $i,1";

            $result2 = $conn->query($sqlSelect);
            $row = $result->fetch_assoc();

            array_push($costs, $row['Sell_Price']);

            print "<tr>";
                print "<td>{$row['ItemID']}</td>";
                print "<td>{$row['Description']}</td>";
                print "<td>R{$row['Cost_Price']}</td>";
                print "<td>{$row['Quantity']}</td>";
                print "<td>R{$row['Sell_Price']}</td>";
                print '<td><img src="images/'.$row['ItemID'].'.png" alt="Sorry" style="width:160px;height:96px;"></td>';
                print '<td><input type = "submit" name = "btnAddToCart" value = "Add To Cart" class = "submit"></td>';
            print "</tr>";
        }
        print "</table>";

如您所见,我有一个名为 btnDisplay 的按钮,它输出一个包含一堆数据行的表格。总会有 15 行,每行在最后一列中都有一个按钮,称为 btnAddToCart。当点击 btnAddToCart 时,我需要知道使用 PHP 是从哪一行点击的。

【问题讨论】:

    标签: php html css mysql


    【解决方案1】:

    使用按钮而不是输入。然后你可以给它一个与按钮中的文本不同的值。

    print "<button type = 'submit' name = 'btnAddToCart' value = '{$row['ItemID']}" class = 'submit'>Add To Cart</button";
    

    那么在 PHP 中,$_POST['btnAddToCart'] 的值将是他们点击的项目 ID。

    【讨论】:

    • 你挡住了 Roomba 的路
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 2013-11-21
    • 1970-01-01
    相关资源
    最近更新 更多