0

I have input field checkboxes,

<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" />

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

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

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);
        }
      });
  });
});

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); 


          }

        }

What did I missed out? Thank you.

4
  • check the browser console for the ajax post data is sent correctly and also check in php end with print_r($_POST) Commented Oct 23, 2018 at 7:37
  • Hi sir, the print_r($_POST['rubricChkbox']) returns the value of array 1 and 2. Commented Oct 23, 2018 at 11:54
  • Hi @jeloneedshelp did you check the three boxes or you only checked two? Commented Oct 25, 2018 at 1:38
  • All checkboxes have already been checked sir. What I did is that I uncheck the level 3 checkbox and get that value. I've already resolved this problem sir but thank you. :) Commented Oct 25, 2018 at 4:36

1 Answer 1

1

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).

In this case

  • 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?
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thank you sir for the advice. The return value of the console.log is 1 and 2. It left out the value 3. And there is not error in my error statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.