简体   繁体   English

PHP多个提交按钮在表中

[英]PHP Multiple Submit Buttons in table

I have been creting a php website that allows users to add comments to an image that is within the system. 我一直在创建一个php网站,允许用户为系统内的图像添加注释。

The way my code currently works is that it gets all of the comments for that image and places them into a table using a while loop: 我的代码目前的工作方式是它获取该图像的所有注释,并使用while循环将它们放入表中:

$commResult = mysql_query("SELECT u.userID, u.USERNAME, c.COMMENT, c.DATE_ADDED, c.ACTIVE, c.id FROM USERS u, COMMENTS c WHERE u.id = c.user_id and c.box_id = $boxId ORDER BY c.DATE_ADDED DESC");

while ($row = mysql_fetch_array($commResult))
    {
        if ($row[4] != 0)
        {
            echo "<tr><td><a href='User.php?uid=$row[0]'>$row[1]</a></td>";
            echo "<td style='text-align:right;'>" . date("d M y g:iA", strtotime($row[3])) ;
            if (isLoggedIn())
                echo " - DELETE";
            echo "</td></tr>";
            echo "<tr><td colspan='2'>" . $row[2] . "</td></tr>";
        }
    }

The problem i am having is that i would like to put a submit button where the word DELETE is. 我遇到的问题是我想在DELETE这个词的位置放一个提交按钮。 This would create a button for each comment row and therefore if clicked the code would not know what button has been pressed. 这将为每个注释行创建一个按钮,因此如果单击该代码将不知道按下了什么按钮。 Is there anyway to get arround this so each button has a individual ID so when the code is submitted it knows what the id of the comment is and therefore i am able to process a delete on the database table for that comment ID. 无论如何都要得到这个,所以每个按钮都有一个单独的ID,所以当提交代码时,它知道注释的id是什么,因此我能够在数据库表上处理该注释ID的删除。

I have tried adding this piece of code where the word DELETE is: 我试过在DELETE这个词的地方加上这段代码:

if (isLoggedIn())
                echo " - <button type='submit' name='delCom_sub' value='$row[5]' >X</button>";

However when i try to handle the button click using the following code: 但是,当我尝试使用以下代码处理按钮时:

if (!empty($_POST['delCom_sub']))
{
    echo "test";
}

IF i click a button the word "test" is never displayed. 如果我点击一个按钮,则永远不会显示“测试”字样。

It can be done with a separate form for each comment row. 可以使用单独的表单为每个注释行完成。 The ID of the comment is stored in a hidden field. 注释的ID存储在隐藏字段中。 Using this method you need to remove any parent form to prevent nested forms. 使用此方法,您需要删除任何父窗体以防止嵌套窗体。

        if (isLoggedIn())
        {
            echo '<form action="delete.php" method="post">
                  <input type="hidden" name="id" value="' . (int)$row['id'] . '" />
                  <input type="submit" value="Delete" />
                  </form>';
        }

On the page that you post to, ie delete.php : 在您发布到的页面上,即delete.php

if(isset($_POST['id']) && isLoggedIn())
{
    // do the delete with $_POST['id']
}

Other than that, you could do it with Javascript by populating a hidden field when a button is clicked. 除此之外,您可以通过在单击按钮时填充隐藏字段来使用Javascript来完成此操作。 Another option would be to store the comment ID in a submit button's name attribute, doing that you would have to loop over the post variables and parse out the ID. 另一种选择是将注释ID存储在提交按钮的name属性中,这样做就必须循环遍历post变量并解析ID。

Example using the button name : 使用按钮name示例:

if (isLoggedIn())
            echo " - <input type='submit' name='delete_" . (int)$row['id'] . "' value='Delete' />";

On the receiving page: 在接收页面上:

if($_SERVER['REQUEST_METHOD'] == 'POST' && isLoggedIn())
{
    foreach($_POST as $key => $value)
    {
        if(strpos($key, 'delete_') === 0)
        {
            $id = substr($key, 7);
            // do the delete for $id
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM