使用foreach循环,jquery和ajax插入多个复选框值 [英] multiple checkbox values inserting using foreach loop, jquery and ajax

查看:93
本文介绍了使用foreach循环,jquery和ajax插入多个复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有输入字段复选框,

<input value="1" name="rubric_chkbox[]" type="checkbox" class="checkbox" />
<input value="2" name="rubric_chkbox[]" type="checkbox" class="checkbox" />
<input value="3" name="rubric_chkbox[]" type="checkbox" class="checkbox" />

我的问题是,如果我选中了所有复选框,则在我的foreach循环中插入查询,

My problem is that in my foreach loop insert query if I checked all the checkbox,

它将只在数据库中插入值1和2,而忽略值3.

It will just insert value 1 and 2 in my database it ignores the value 3.

我正在使用jquery和ajax来获取复选框.

I'm using jquery and ajax to get the checkboxes.

var rubricChkbox = new Array();
    $(".rubricChkbox:checked").each(function() {
         rubricChkbox.push($(this).val()); 
    });

    console.log(rubricChkbox);

    $.ajax({
        url: "Queries/save.php",
        type: "POST",
        data: {
          "rubricChkbox":rubricChkbox
        },
        success: function(yey){
          console.log(yey);
          alert(yey);
        }
      });
  });
});

这是我的save.php,

And this is my save.php,

if (isset($_POST['rubricChkbox']) || isset($_POST['uncheked']) || isset($_POST['user_id'])) {
$rubric_value = $_POST['rubricChkbox'];
    $IDuser = $_POST['user_id'];

        foreach($rubric_value as $rubric_check) {

          $sql_check = "SELECT raw_selected_rubric FROM rubric_selected INNER JOIN cmat_composition ON rubric_selected.ID_cmat = cmat_composition.ID_cmat WHERE rubric_selected.ID_users = '$IDuser' AND raw_selected_rubric = '$rubric_check' 
          AND rubric_selected.Saved = '1'";
          $result_check = mysqli_query($conn,$sql_check);

          if (mysqli_num_rows($result_check) <= 0){

            $sql_raw = "INSERT INTO rubric_selected (raw_selected_rubric, Saved, ID_users)

          VALUES  ('$rubric_check', '1', '$IDuser')";

          mysqli_query($conn, $sql_raw); 


          }

        }

我错过了什么?谢谢.

推荐答案

对您最好的建议是,如果某些方法无法按您希望的方式工作,则应检查每一步会发生什么,直到发现问题为止.这不仅适用于此问题,还适用于任何编程问题(以及许多其他问题).

The best advice for you is that if something doesn't work the way you want, you should examine what happens at each step until you find the problem. This doesn't only apply to this question, but to any programming problem (and many others).

在这种情况下

  • 从浏览器发送的数据是否正确?您已经有语句console.log(rubricChkbox);.您检查了该值吗?您为什么不将其包含在问题中?
  • 正确的数据是否到达PHP脚本?使用类似print_r ($_POST);的东西.
  • SQL INSERT取决于SQL SELECT的结果.您是否检查过这种情况是否成立,否则您的代码甚至都不会尝试插入.
  • INSERT语句返回一些错误吗?
  • Is the data sent from the browser correct? You already have the statement console.log(rubricChkbox);. Did you check that value? Why didn't you include it in the the question?
  • Does the correct data arrive at the PHP script? Use something like print_r ($_POST);.
  • The SQL INSERT depends on the result of a SQL SELECT. Did you check that this condition is true, because otherwise your code wont even try to insert.
  • Does the INSERT statement return some error?

这篇关于使用foreach循环,jquery和ajax插入多个复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆