I am trying to implement a confirmation modal in my PHP page that will submit a form when the user confirms an action (like deletion). I want to avoid using custom JavaScript for the submission and rely mainly on HTML and PHP to handle the form submission.
Here’s the flow I want:
- User clicks a button to open the modal.
- The modal asks for confirmation with two options: Cancel and Yes, Submit.
- Clicking “Yes, Submit” should submit the form and trigger PHP logic, without using JavaScript to handle the submission.
Here’s the sample code I am working with:
<form id="generic_form" method="post">
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#confirm_modal">
**Delete Item**
</button>
</form>
<!-- Confirmation Modal -->
<div class="modal fade" id="confirm_modal" tabindex="-1" role="dialog" data-backdrop="static" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">**Are you sure?**</h4>
</div>
<div class="modal-body">
**This action will remove the item permanently.**
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">**Cancel**</button>
<button type="submit" class="btn btn-danger" name="btn_confirm_delete" value="submit" data-dismiss="modal">
**Yes, Submit**
</button>
</div>
</div>
</div>
</div>
// HERE : This code is Not Executing
if (isset($_POST["btn_confirm_delete"])) {
// Call the function to handle the deletion
**handle_deletion_function**();
}
As far as I know, when a submit button is clicked, the form containing the button is submitted, and its $_POST data is sent to the server. However, in this case, we are not directly submitting the form, so the page does not reload, which is the default behavior when a form is submitted. Also I do not want a separate JavaScript function to handle this submission.