【问题标题】:Auto submitting Checkbox to save to MySQL自动提交复选框以保存到 MySQL
【发布时间】:2017-10-26 20:15:12
【问题描述】:

我的网站上有一个页面,每个拥有帐户的用户都有 20 个复选框,我希望能够在更改复选框以将其保存到数据库时提交该复选框。很难解释,但我会附上代码、HTML 输出和 DB 的截图:

获取盒子的 PHP 代码是:

<?php  
for ($x = 1; $x <= 16; $x++) {
            $result0 = $db->query("SELECT * FROM View WHERE `UserID` LIKE '" . $x . "'");
            while($row0 = mysqli_fetch_array($result0)) { 
                if($row0['20']=="1"){$checked = "checked";}else { $checked = "";}}
echo "<td class=\"align_center\"><input type=\"checkbox\" name=\"20_" . $x . "\" " . $checked . "/>
";}
?> 

我希望有人可以提供帮助!请!

谢谢

【问题讨论】:

  • 所有复选框都在表单中吗?如果正确,您可以使用 jquey 获取事件click 提交表单
  • @L Barnard 为什么小于等于for ($x = 1; $x &lt;= 16; $x++)
  • 肯先生,你能提供密码吗?
  • Pavan Baddi,这就是它添加多列复选框的方式

标签: php jquery html mysql checkbox


【解决方案1】:

下面的 ajax 请求将在每个复选框更改事件上触发。

您将需要一个 PHP 脚本来接收数据并更新您的数据库。

查看Ajax documentation了解更多详情。

$("input[type='checkbox']").on("change",function(){

  $.ajax({
    url: "someFileToUpdateDB.php",
    data: $("#myForm").serialize(),
    success: function(){
      console.log("Change saved.");
    },
    error: function(request,status,error){
      console.log("ERROR: "+error);
    }
  });

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2013-08-10
    相关资源
    最近更新 更多