Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Modal before form Submit

I'm new to Modals, I have a Form and when the user clicks submit, It will show a Modal confirming if the user wants to submit, the modal also contains the user input from the form fields. I searched all over the internet but can't find the right one on my needs. And all I see is that they tag the click event to open modal on a a link. i have a input type submit. Can you give examples or ideas? Thanks! Here's my sample form.

<form role="form" id="formfield" action="inc/Controller/OperatorController.php" method="post"  enctype="multipart/form-data" onsubmit="return validateForm();"> <input type="hidden" name="action" value="add_form" />          <div class="form-group">          <label>Last Name</label><span class="label label-danger">*required</span>          <input class="form-control" placeholder="Enter Last Name" name="lastname" id="lastname">        </div>          <div class="form-group">           <label>First Name</label><span class="label label-danger">*required</span>           <input class="form-control" placeholder="Enter First Name" name="firstname" id="firstname">        </div>    <input type="submit" name="btn" value="Submit" id="submitBtn" class="btn btn-default" data-confirm="Are you sure you want to delete?"/>   <input type="button" name="btn" value="Reset" onclick="window.location='fillup.php'" class="btn btn-default" data-modal-type="confirm"/> </form> 
like image 974
user3651491 Avatar asked May 21 '14 06:05

user3651491


People also ask

How do I submit a bootstrap modal form?

You CAN include a modal within a form. In the Bootstrap documentation it recommends the modal to be a "top level" element, but it still works within a form. You create a form, and then the modal "save" button will be a button of type="submit" to submit the form from within the modal.

How do I keep modal open after submitting?

The goal is to submit the data to our db when the submit button is clicked and then hide the form and display a thank you within the same modal. Here's the basics of the code for the modal: <div class="modal fade" id="formModal" tabindex="-1" role="dialog" aria-labelledby="formModalLabel" aria-hidden="true">

How do I make a bootstrap modal scrollable?

Bootstrap 4.3 added new built-in scroll feature to modals. This makes only the modal-body content scroll if the size of the content would otherwise make the page scroll. To use it, just add the class modal-dialog-scrollable to the same div that has the modal-dialog class.

How do you call a modal from another page?

To make it work, first remove the data-target and data-toggle attributes from the link. You can leave the href attribute there. Using jQuery we can add a click listener to the <a> element, but we do not want to directly go to the page indicated by the href attribute so we use preventDefault() .


1 Answers

So if I get it right, on click of a button, you want to open up a modal that lists the values entered by the users followed by submitting it.

For this, you first change your input type="submit" to input type="button" and add data-toggle="modal" data-target="#confirm-submit" so that the modal gets triggered when you click on it:

<input type="button" name="btn" value="Submit" id="submitBtn" data-toggle="modal" data-target="#confirm-submit" class="btn btn-default" /> 

Next, the modal dialog:

<div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">     <div class="modal-dialog">         <div class="modal-content">             <div class="modal-header">                 Confirm Submit             </div>             <div class="modal-body">                 Are you sure you want to submit the following details?                  <!-- We display the details entered by the user here -->                 <table class="table">                     <tr>                         <th>Last Name</th>                         <td id="lname"></td>                     </tr>                     <tr>                         <th>First Name</th>                         <td id="fname"></td>                     </tr>                 </table>              </div>              <div class="modal-footer">                 <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>                 <a href="#" id="submit" class="btn btn-success success">Submit</a>             </div>         </div>     </div> </div> 

Lastly, a little bit of jQuery:

$('#submitBtn').click(function() {      /* when the button in the form, display the entered values in the modal */      $('#lname').text($('#lastname').val());      $('#fname').text($('#firstname').val()); });  $('#submit').click(function(){      /* when the submit button in the modal is clicked, submit the form */     alert('submitting');     $('#formfield').submit(); }); 

You haven't specified what the function validateForm() does, but based on this you should restrict your form from being submitted. Or you can run that function on the form's button #submitBtn click and then load the modal after the validations have been checked.

DEMO

like image 112
AyB Avatar answered Sep 28 '22 03:09

AyB