1

I have a checkbox:

<input value="".$count_FRID."" name="rubric_chkbox[]" />

I'm using jquery and ajax to get the data and use foreach loop to insert it to my database as follows:

var rubricChkbox = new Array();
$("input:checked").each(function() {
    rubricChkbox['rubric_chkbox[]'].push($(this).val()); 
});
console.log(rubricChkbox);

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

For deleting of checkbox:

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

jQuery and AJAX:

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

$(".checkbox:checkbox:not(:checked)").each(function() {
    uncheked.push($(this).val()); 
});
$.ajax({
    url: "Queries/save.php",
    type: "POST",
    data: {
        "rubricChkbox":rubricChkbox
    },
    success: function(yey){
    console.log(yey);
        alert(yey);
    }
});

For example if 1 and 2 values are inserted in my database, and if I unchecked checkbox 1 and 2, shouldn't the code above work for deleting rows?

I'm getting below error:

Uncaught TypeError: Cannot read property 'push'

EDIT, save.php:

if (isset($_POST['rubricChkbox']) || isset($_POST['uncheked']) || isset($_POST['user_id'])) {
    $rubric_value = $_POST['rubricChkbox'];
    $rubric_uncheck_value = $_POST['uncheked'];
    $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); 

        }
    }
}

1 Answer 1

1

You don't need to put the brackets [ ] in your array variable. Use rubricChkbox.push($(this).val());

var rubricChkbox = new Array();
$("input:checked").each(function() {
       if($(this).attr("id") == "option1"){
          // if the id of checkbox is option1 then push it
          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);
    }
});

Edit: For your additional question, yes you can add a class specific for your checkboxes and loop through them instead;

Run the code below.

$("button").click(function() {
  var unchecked = new Array();

  $(".checkbox:not(:checked)").each(function() {
    unchecked.push($(this).val());
  });

  console.log(unchecked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="1" class="checkbox" type="checkbox" name="rubric_chkbox[]" />
<input value="2" class="checkbox" type="checkbox" name="rubric_chkbox[]" />
<button>Validate</button>

Sign up to request clarification or add additional context in comments.

10 Comments

Can I have if/else condition inside in this $("input:checked").each(function() { (make this and if/else condition) rubricChkbox.push($(this).val()); }); sir?
@jeloneedshelp yes you can. Updated the answer. Added an if that would check the id of the checkbox
Would it work sir if I added class="checkBoxRubric" in my input field and get every checked checkboxes that has a class checkBoxRubric?
I'm sorry if I have a lot of questions sir. It's because I have another foreach loop that I uncheck that chekcbox it would delete that row in my database. I will edit my question for the delete query.
@jeloneedshelp please create a new question then comment it here so I would check, thanks
|

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.