Prajna Hegde
Prajna Hegde

Reputation: 740

How to control checked/unchecked for check box based on javascript alert(ok/cancel)?

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>

Upvotes: 0

Views: 1491

Answers (1)

Komal
Komal

Reputation: 1097

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>

Upvotes: 3

Related Questions