JamMan9
JamMan9

Reputation: 776

Form inside modal not working

Good morning,

I'm trying to delete a record from a database on a server. I have a form which displays the table, and has a delete button. When the user clicks the delete button, a bootstrap modal appears and asks them to confirm the delete. What I want to happen is; when the user clicks delete (within the modal), it runs the PHP code at the top of the page and deletes the record.

For some reason, nothing happens when I click the button, and I can not figure out why.

if(isset($_POST['deleteCategory'])) {
    $sql = "DELETE FROM categories WHERE name = :categoryToDelete;";
    $stmt = $pdo->prepare($sql);
    $stmt->bindValue(':categoryToDelete', $_POST['categoryToDelete']);
    $stmt->execute();
}
?>

<?php foreach ($result as $row) { ?>
    <tr>
        <td><?php echo $row->name; ?></td>
        <td><?php echo $row->description; ?></td>
        <td id="editRemoveButtonsCell">
            <button id="editButton" type="button" class="btn btn-outline-warning">Edit</button>
            <button id="removeButton" type="button" class="btn btn-outline-danger" data-toggle="modal" data-target="#removeCategoryModal<?php echo $row->name;?>">Remove</button>
        </td>
    </tr>

    <!-- Delete Modal -->
    <div class="modal" id="removeCategoryModal<?php echo $row->name;?>">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- Modal Header -->
                <div class="modal-header">
                    <h4 class="modal-title">Delete Category <?php echo $row->name?></h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>

                <!-- Modal body -->
                <div class="modal-body">
                                        Are you sure you want to delete category <?php echo $row->name?>? All items associated with this category will also be deleted. <b>This cannot be undone!</b>
                </div>

                <!-- Modal footer -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-cancel" data-dismiss="modal">Close</button>
                    <form action="#" method="post" role="form">
                                            <?php $_POST['categoryToDelete'] = $row->name; ?>
                                            <button type="submit" name="deleteCategory" class="btn btn-danger" data-dismiss="modal">Delete</button>
                    </form>

                </div>

            </div>
        </div>
    </div>
<?php } ?>

Any further info needed, please feel free to ask. Thanks!

Upvotes: 0

Views: 818

Answers (2)

sree
sree

Reputation: 389

In your modal footer, keep a input type hidden variable for testing.

<input type="text" name="catname" value="<?php echo $row->name; ?>">

and check whether this is displaying in the modal box, when clicked on delete button or not.

if it is displaying in the modelbox form, then submit the form. In your submit code, keep a die; statement for debugging like below

isset($_POST['deleteCategory'])) {
      print_r($_POST);
}

check, if the $_POST['catname'] is submitted or not. if it is submitted, your query will definitely work.

Upvotes: 0

Federico klez Culloca
Federico klez Culloca

Reputation: 27119

<?php $_POST['categoryToDelete'] = $row->name; ?>

This overwrites $_POST['categoryToDelete'] at every cycle. It's not creating a different parameter for each request. Also, as soon as the page is rendered it will be gone, so it won't be passed as you expect when you submit the form.

Your best bet is to replace it with an hidden input like this

<input type="hidden" name="categoryToDelete" value="<?php echo $row->name; ?>">

Upvotes: 1

Related Questions