Reputation: 51
I have a form to update mysqli database with php codes. The codes are fine and when I submit the form the database updates and that means it works fine. But I want to receive a confirmation bootstrap modal before submitting the form. For it I used the following codes. Here the modal opens, but when clicking the OK button it's not working.
<button class="btn btn-danger btn-lg" name="submit" value="submit" type="submit" data-condition="salaryinfo" data-toggle="modal-confirm" data-message="Are you sure?" data-title="Hi you!" data-target="#submit-confirm">Submit</button>
</form>
<!-- Modal -->
<div class="modal fade" id="submit-confirm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title"></h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-confirm="modal">OK</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-1.12.0.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<!-- For modal -->
<script src="js/confirm-bootstrap.js"></script>
<script type="text/javascript">
function salaryinfo(){
return true;
};
$(document).ready(function(){
//modal delete confirm
$('button[data-toggle="modal-confirm"]').click(function(event) {
event.preventDefault();
var self = $(this);
var message = self.data('message');
var title = self.data('title');
var target = $(self.data('target'));
var condition = self.data('condition');
if( target.length == 1) {
target.find('.modal-title').html(title);
target.find('.modal-body').html(message);
var showModal = true;
var fn = window[condition];
if(typeof fn === 'function') {
showModal = fn(condition);
}
if( showModal ) {
target.on('shown.bs.modal', function(e) {
target.find('button[data-confirm="modal"]').click(function(e){
e.preventDefault();
var parentForm = self.closest('form');
console.log(parentForm.html());
if( parentForm.length == 1 ) {
parentForm.submit();
}
});
});
target.modal({ show: true });
};
};
});
});
</script>
Any suggestion for a working modal?
Upvotes: 3
Views: 2616
Reputation: 700
You can bind the form submission to the Ok button instead of the Submit button.
Click on the Submit
button: open the modal.
Click on the Ok
button: submit the form
Assign an ID to your form <form id='my_form'>
.
Assign an ID to your Ok
button
<button id='my_ok_button' type="button" class="btn btn-primary" data-confirm="modal">OK</button>
Bind the form submission to your Ok
button
<script type='text/javascript'>
$(document).on('click', '#my_ok_button', function() {
$('#my_form').submit();
})
</script>
You need to include jQuery for this script to work.
Upvotes: 2
Reputation:
You are already submitting the form before the modal opens because your button has the value submit:
<button class="btn btn-danger btn-lg" name="submit" value="submit" type="submit" data-condition="salaryinfo" data-toggle="modal-confirm" data-message="Are you sure?" data-title="Hi you!" data-target="#submit-confirm">Submit</button>
So just make that a normal button with a data toggle to your modal.
And then make the confirm button on your modal, to have the value of submit.
You also need to include your modal within your <form> </form>
Upvotes: 1