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'];

?> 

Recommended Answers

All 3 Replies

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');

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

pritaeas Thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.