0

Hi
I am trying to update the ON / OFF status through the below code.

If i change the OFF status to ON, its getting a value is OFF. can any one please help to fix the issue.

toggle.php

<div id="setQuickVar1"> <input type="checkbox" data-switchery <?php if($SCYQ64EV01=='Y') echo 'checked'; ?> /> </div> <span class="uk-form-help-block"> <div id="resultQuickVar1"></div> </span> <script type="text/javascript">
$(document).ready(function() { 
     $('#setQuickVar1').on('click', function() {
        var checkStatus = this.checked ? 'ON' : 'OFF';
         alert(checkStatus);
        $.post("engineDBUpdate.php", {"quickVar1a": checkStatus}, 
        function(data) {
            $('#resultQuickVar1').html(data);
        });
     });
});
</script>

EngineDBUpdate.php

<?php
    if (isset($_POST['quickVar1a']))
        echo $quickVar1a = $_POST['quickVar1a'];

?> 

Edited by pritaeas: Moved to web dev.

3
Contributors
3
Replies
21
Views
1 Year
Discussion Span
Last Post by rpv_sen
0

If i change the OFF status to ON, its getting a value is OFF

That's because you get the value at the time of your click, and not the new value. So, just change your check:

var checkStatus = this.checked ? 'OFF' : 'ON';

or use:

var checkStatus = $(this).attr('value');

Edited by pritaeas

0

Or you can just use the change listener instead of the click. When the change is fired the checkbox state has already been updated.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.