0

I'm trying to make a confirmation popup before user submits the form with CodeIgniter but the trigger/submit part is not working. It asks for confirmation but doesn't submits the form.

My HTML:

<?php
echo form_open(site_url("action"), array('id' => "order" , )) ?>
<input type="text" class="form-control" name="anything" value="">
<button type="submit" id="btn-submit" class="btn btn-danger" class="form-control">Submit</button>
<?php echo form_close() ?>

And Here's the Javascript

$('#btn-submit').on('click',function(e){
e.preventDefault();
var form = $(this).parents('form');
swal({
  title: "Are you sure?",
  text: "You will not be able to recover this imaginary file!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, delete it!",
  closeOnConfirm: false
}, function(isConfirm){
  if (isConfirm) form.submit();
});
});

I've also tried targeting/selecting form id instead button but same issue. Resources are loaded properly.

CodeWriter
  • 107
  • 1
  • 1
  • 13

2 Answers2

0

Change it:

var form = $(this).parents('form');

to

var form = $(this).parent('form');

and try again.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0

I was actually using wrong function. The function I used was from sweetalert while I loaded sweetalert2. I changed the code from

swal({
  {... closeOnConfirm: false},
  function() {
   // Function
  }
});

to

swal({
  ...
  showLoaderOnConfirm: true,
  preConfirm: function() {
    //function
}).then(function() {
  swal('Processing');
});

and it's working

CodeWriter
  • 107
  • 1
  • 1
  • 13