chudst
chudst

Reputation: 149

Sweetalert2 doesn't return true/false

I'm trying to work with the SweetAlert2. It works, when I don't want to send some data through $_POST. But when I want to send data through $_POST, the sweetalert just appears on less then one second, then disappers and regardless of user choice the form is send.

How can I modify my code, when I'd like to have a regular window-confirmation and when the user click on "delete", the form will be send, otherways when he click on "cancel", the form will be not send?

if(isset($_POST['submit'])) {  
  echo 'Your form was submitted.';
}

<form action="#" method="post>                
 <label>
   <span>Username</span>
   <input type="text" value="" name="username" />
 </label>
 <input type="submit" name="submit" value="Register!" id="test-1" />    
</label>     
</form>



<script>
 document.querySelector('#test-1').onclick = function(){
  swal({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!'
  }).then(function () {
    swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
      )
  })

     //IF SWAL => DELETE -> send form
     //ELSE IF SWAL => CANCEL -> do nothing 
     //when I put here "return false;", then sweetalert is working, but it doesn't send the form
</script>

Solution thanks to Novice:

<?php if(isset($_POST['send']))
{echo 'Your form was submitted.';} ?>

<form id='myfrm' action="#" method="post">   
 <label>
   <span>Username</span>
   <input type="text" value="" name="username" />
   <input type="hidden" name="send" value="send">
 </label>
</form>
 <button value="Register!" id="test-1"> submit </button>

<script>
document.querySelector("#test-1").onclick = function(){
  swal({
    title: "Are you sure?",
    text: "You won't be able to revert this!",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#3085d6",
    cancelButtonColor: "#d33",
    confirmButtonText: "Yes, delete it!"
  }).then(function () {
   document.querySelector("#myfrm").submit();    
  })
}      
</script>

Upvotes: 2

Views: 3115

Answers (1)

Vinay
Vinay

Reputation: 7676

Rather than using handler you can use handler on form , much better that way

Give form an id also change your submit button to a normal button coz we will submit(or not submit) manually depending on user's choice

<form id='myfrm' action="#" method="post">    <!-- u missed quote here -->            
 <label>
   <span>Username</span>
   <input type="text" value="" name="username" />
 </label>
 <!-- EDIT : removed (not working)
 <input type="button" name="submit" value="Register!" id="test-1" />    
-->
<input type="hidden" name="submit" value="submit"/>
</label>     
</form>
<!-- EDIT : added -->
<button name="submit" value="Register!" id="test-1"> submit </button>



<script>
 document.querySelector('#test-1').onclick = function(){
  swal({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!'
  }).then(function () {

     // swal( 'Ready?!', 'Your form is going to be submitted.' 'success');
     // if you want to show alert that form is going to submit you may un-comment above line
    document.querySelector('#myfrm').submit(); // manully submit 
  })

Upvotes: 1

Related Questions