简体   繁体   中英

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.

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:

$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. 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.

I have tried adding this piece of code where the word DELETE is:

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. 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 :

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. 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.

Example using the button 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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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