【问题标题】:Checkboxes based on user id it save's only one id from all the checkboxes基于用户 id 的复选框,它只保存所有复选框中的一个 id
【发布时间】:2018-09-07 21:00:50
【问题描述】:

我有一个基于用户 ID 的带有 10 个复选框的表单,但是当我选中数据库中的一个框时,我看到只有用户 ID 1 复选框正在保存。请查看下面的代码,感谢您的帮助!

HTML:

<form method="post" action="" id="prezente" uid="<?php echo $row['id'];?>">

     <input type="checkbox" name="day1" id="day1" value="1" class="form-check-input">

     <input type="checkbox" name="day2" id="day2" value="1" class="form-check-input">

     <input type="checkbox" name="day3" id="day3" value="1" class="form-check-input">

And so on until day10 ..
</form>

Javascript:

$(document).ready(function(){

$('#day1,#day2,#day3').click(function(){

   var id = $('#prezente').attr('uid');
   var day1 = $('input[name=day1]:checked').val();
   var day2 = $('input[name=day2]:checked').val();
   var day3 = $('input[name=day3]:checked').val();

            $.ajax({
                url: 'sql/add-days.php?uid='+id,
                type: 'post',
                data: { day1: day1,day2: day2,day3: day3 },
                success:function(data){
                  alert("success");
                }
            });
        });
});

add-days.php 文件:

// Make a MySQL Connection
$db = mysqli_connect("localhost", "stelelea_anapp", "fidodido", "stelelea_anadanceapp");

$day1 = $_POST['day1'];
$day2 = $_POST['day2'];
$day3 = $_POST['day3'];

$uid = $_GET['uid'];

 $query = mysqli_query($db,"UPDATE `inscrieri` SET day1='$day1',day2='$day2',day3='$day3' WHERE id = $uid");

这是我的复选框的样子,一个圆圈代表一天:

【问题讨论】:

  • 所以你是说如果你点击的第一个复选框是#4,它不会更新它们中的任何一个?
  • 是的,完全正确..
  • 我在您的问题中看不到任何会导致这种情况发生的内容。这正是你在js和php中的代码吗?
  • 虽然有一个问题。 uid="&lt;?php echo $row['id'];?&gt;"&gt; 让您感觉页面上可能有多个这样的表单?
  • 是的,这是代码,如果我选中 user id 2 复选框,它会说用户的 id 为 1,依此类推......在所有用户上它只抓取 id 1

标签: javascript php jquery html checkbox


【解决方案1】:

修改了 html 和 javascript 以使用类,以及上下文查找。

$(document).ready(function() {
  //bind on the shared class so you don't have to look for multiple ids or classes
  $('.day').click(function() {
    //get the form that is the parent of the day clicked
    var $form = $(this).closest('.prezente');
    //get the id of the form
    var id = $form.attr('uid');
    //get the days that belong to the form
    //if the field is not checked, it would not be found and would return
    //  undefined for the val(), in which case we can default to 0
    var day1 = $form.find('.day1:checked').val() || 0;
    var day2 = $form.find('.day2:checked').val() || 0;
    var day3 = $form.find('.day3:checked').val() || 0;

    $.ajax({
      url: 'sql/add-days.php?uid=' + id,
      type: 'post',
      data: {
        day1: day1,
        day2: day2,
        day3: day3
      },
      success: function(data) {
        alert("success");
      }
    });
  });
});
<form method="post" action="" class="prezente" uid="<?php echo $row['id'];?>">
  <input type="checkbox" name="day1" value="1" class="day day1 form-check-input">
  <input type="checkbox" name="day2" value="1" class="day day2 form-check-input">
  <input type="checkbox" name="day3" value="1" class="day day3 form-check-input">
</form>

【讨论】:

    猜你喜欢
    • 2022-09-22
    • 2010-11-05
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2021-12-24
    相关资源
    最近更新 更多