Javascript 如何将复选框的值更新到数据库1(如果选中)和0(如果未选中)?

Javascript 如何将复选框的值更新到数据库1(如果选中)和0(如果未选中)?,javascript,arrays,database,checkbox,Javascript,Arrays,Database,Checkbox,这是我的数据库 请浏览文档并了解表单的工作方式。这里有很多例子,我推荐Head First系列书,你可以找到一个很好的例子 下面是解决您的问题的示例代码 创建一个名为example.html的文件并保存此内容 <html> <head> <!-- link to jquery lib --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery

这是我的数据库

请浏览文档并了解表单的工作方式。这里有很多例子,我推荐Head First系列书,你可以找到一个很好的例子

下面是解决您的问题的示例代码

创建一个名为example.html的文件并保存此内容

    <html>
    <head>
<!-- link to jquery lib -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
    <form action="" method="POST">

    <h4 id="result"></h4>

    <div class="container">
      <h2 align="center">Table App Feature</h2>   
            <label> Name </label>
        <input type='text' name='name' id='name' value=''/>
      <table id="appFeature" class="table table-hover" align="center" style="width:500px;margin:auto;">
        <thead>
          <tr>
            <th>Firstname</th>
            <th>Please check to enable the features</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Smarthome</td>
            <td>
<!-- checkbox for smarthome value -->
              <input type="checkbox" class="get_value" id='smarthome'/>
            </td>
          </tr>
        <tr>
            <td>Intercom</td>
            <td>
              <input type="checkbox" class="get_value" id='intercom'/>
            </td>
          </tr>
        </tbody>
      </table><br />
      <div align="center">
        <label>check if you want to update, unckeck if you want to insert</label>
        <input type="checkbox" class="get_value" id='update'/>
        <br>
<!-- button name -->
            <button type="button" name="submit" id="submit">Update or Insert</button>
      </div>
    </div>
    </form>
    </body>
    <script type="text/javascript">
      $(document).ready(function(){
         $('#submit').click(function(){
// get the value is checked from the form
           $.ajax({
           url: "insert.php",
           method: "POST",
           data:{intercom: $('#intercom').is(':checked'),smarthome: $('#smarthome').is(':checked'), name: $('#name').val(), update: $('#update').is(':checked')},
           success:function(data){
           $('#result').html(data);
           }
           });
         });
      });
    </script>
    </html>

表格应用程序功能
名称
名字
请选中以启用这些功能
智能家居
对讲机

检查是否要更新,如果要插入,请取消锁定
更新或插入 $(文档).ready(函数(){ $(“#提交”)。单击(函数(){ //获取从表单中选中的值 $.ajax({ url:“insert.php”, 方法:“张贴”, 数据:{intercom:$('#intercom')。is(':checked'),smarthome:$('#smarthome')。is(':checked'),name:$('#name').val(),update:$('#update')。is(':checked'), 成功:功能(数据){ $('#result').html(数据); } }); }); });
php文件名为insert.php,如下所示。确保这两个文件位于同一目录中,并且位于apache服务器内。(localhost public directory)


您使用的是哪种服务器端编程语言?如果是PHP,请检查以下内容:您可以从提供最小的工作代码片段开始,而不是代码片段的图像。请不要将链接放在屏幕截图上。您可以在这里添加代码。对我们来说,给出一个答案很容易answer@vbRocks我正在使用php。。
<?php
$servername = "localhost";
$username = "YOURDBUSER";
$password = "YOURDBPASSWORD";
$dbname = "databaseappfeature"; // db name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql ;

if($_POST['update']){
    $sql = "UPDATE appfeature SET smarthome=".$_POST['smarthome'].", intercom=".$_POST['intercom']." WHERE name='".$_POST['name']."'";
}else{
    $sql = "INSERT INTO appfeature (name, smarthome, intercom) VALUES ('".$_POST['name']."',".$_POST['smarthome'].",".$_POST['intercom'].")";
}

if ($conn->query($sql) === TRUE && !$_POST['update']) {
    echo "New record created successfully";
}else if($conn->query($sql) === TRUE && $_POST['update']){
    echo "record updated";
}else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>