尝试通过使用AJAX PHP切换开关来更新Mysql

时间:2018-11-14 09:11:19

标签: php html mysql ajax

每次尝试打开和关闭按钮时,我都试图使用Ajax PHP更新MySQL数据库,而无需重新加载页面。

但是,它不会更新数据库中的数据。

HTML

<input type="checkbox" id = "checkStatus" name="checkStatus" class="switcher-input" checked="">

PHP

<?php
session_start();
include_once 'dbh.inc.php'; // has $conn variable.

if(isset($_POST['checkStatus'])){
    if($_POST['checkStatus']=='ON'){
    $status = 1; //to save in DB as boolean
    }else if($_POST['checkStatus']=='OFF'){
    $status = 0;
    }
  $sql = "UPDATE `recipe` SET `status`=$status WHERE creator='$uid'";
    //nev
  mysqli_query($conn,$sql);
    }
?>

JS

    $(document).ready(function() {
     $('#checkStatus').on('click', function() {
        var checkStatus = this.checked ? 'ON' : 'OFF';
         alert(checkStatus);
        $.post("loginsystem/includes/recipe.inc.php", {"checkStatus": checkStatus},
        function(data) {
            $('#checkStatus').html(data);
        });
     });
});

0 个答案:

没有答案