0

I have a javascript code to call a confirm alert of javascript when check box is selected or deselected. If the user click on ok of the alert it will call another php file where in it will update the result to the database, But When I click on Cancel the check box will be clicked which should not be performed.

If I click on cancel there should not be action performed on check box.

enter image description here

    <script>
        function ConfirmActiveInactive(){
            return confirm("Activate/Deactivate user?");
        }
    </script>
    <script>
        $(document).ready(function(){
            $("#activeinactive").on("change", "input:checkbox", function(){
                $("#activeinactive").submit();
            });
        });
    </script>

    <form method="post" id="activeinactive" action="activeInactive.php" onsubmit="return ConfirmActiveInactive()">
    <?php
    while($userdetails = mysqli_fetch_array($user_details, MYSQLI_ASSOC)){
        echo '<tr>';
        echo '<td>
              <span class="custom-checkbox">';
        if($userdetails['is_active']==true){
            echo '<input type="checkbox" name="options[]" value="'.$userdetails['member_id'].'" checked=checked>
                <label></label>';
        }
        else{
            echo '<input type="checkbox" name="options[]" value="'.$userdetails['member_id'].'">
                  <label></label>';
        }
        echo '</span>
            </td>';
        echo '</tr>';
    }
    ?>
   </form>
Liam
  • 27,717
  • 28
  • 128
  • 190
Prajna Hegde
  • 670
  • 4
  • 11
  • 31
  • Have you checked: https://stackoverflow.com/questions/9394131/go-to-url-after-ok-button-if-alert-is-pressed ? The approach should be the same. – Martin Jul 10 '18 at 07:09
  • I have problem with cancel, not ok button @Martin – Prajna Hegde Jul 10 '18 at 07:24
  • I don't understand then. The `window.confirm()` function parses a string for a message and returns a boolean, where true is ok, cancel is false. You can then simply do an `if() else()` logic around that. I.e true, do the update, else do nothing. – Martin Jul 10 '18 at 07:39
  • 1
    How that if() and else()?? @Martin – Deepak N Jul 10 '18 at 07:46
  • What is the question here? – Liam Jul 10 '18 at 08:15
  • 1
    _“But When I click on Cancel the check box will be clicked”_ - you mean _checked_? That _has_ already happened, because you are in the `change` event handler, and that event can’t be cancelled. Just check at this point, if the checkbox _is_ checked - and if so, make your script un-check it again …? – CBroe Jul 10 '18 at 08:17
  • 1
    `if(window.confirm('Confirm?')) { // Do the update } else { // Do nothing or set the checkbox to be unchecked via javascript }` – Martin Jul 10 '18 at 08:28
  • okay I will try @Martin – Prajna Hegde Jul 10 '18 at 09:22

1 Answers1

3

Change code to this:

    <script>
        $(document).ready(function(){
            $("#activeinactive").on("change", "input:checkbox", function(e){
                if(ConfirmActiveInactive())
                {
                   $("#activeinactive").submit();
                }
                else
                {
                    if($(this).is(':checked'))
                        $(this).prop('checked', false);
                    else
                        $(this).prop('checked', true);
                }

            });
        });
    </script>
Komal
  • 1,068
  • 12
  • 23
  • It is fine with when I click on ok of the alert, there is problem with cancel of the alert @Komal – Prajna Hegde Jul 10 '18 at 07:25
  • question is edited and it is more clarified now. @Komal – Prajna Hegde Jul 10 '18 at 07:33
  • This should work by the way, which is the same approach I explained in my comments. You basically run a confirm function which acts as a boolean value, true (ok), or false (cancel). If true you run your update, else you do nothing / untick the checkbox. – Martin Jul 10 '18 at 08:33