0

I have this table in my database,

see table image

Now, in the raw_selected_rubric(Level) column that is where I will insert multiple values based on how many checkbox have been check. For example,

checkbox image

Based on the picture I have selected the level 3 and level 4 and when I click submit only the level 3 value is inserted on my database.

inserted checkbox

Input field,

<input class="rubricChkbox" type="checkbox" value="3" />

Jquery and Ajax,

var rubricChkbox = [];
    $('.rubricChkbox').each(function(){  
            if($(this).is(":checked"))  
            {  
                 rubricChkbox.push($(this).val());  
            }  
       });  
       rubricChkbox = rubricChkbox.toString();
$.ajax({
        url: "Queries/save.php",
        type: "POST",
        data: { 
              "rubricChkbox":rubricChkbox
              },
        success: function(yey){
          console.log(yey);
          alert(yey);
        }
      });

Queries/save.php,

if(isset($_POST['rubricChkbox'])) {
$rubric_value = $_POST['rubricChkbox'];

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

        VALUES  ('$rubric_value', '1')";

        $success = mysqli_query($conn, $sql_raw); 
}

What's wrong in my code? I'm sorry I'm still learning jquery and ajax. Thank you for the help.

What I want to happen is that if I selected Level 3 and 4 it both data will be inserted like this,

expected output

3 Answers 3

1

you need to have multiple insert statements like this for example:

if(isset($_POST['rubricChkbox'])) {
$rubric_value = $_POST['rubricChkbox'];
    foreach($rubric_value as $value){
           $sql_raw = "INSERT INTO rubric_selected (raw_selected_rubric, Saved, ID_cmat, ID_users)

            VALUES  ('$value', '1')";

            $success = mysqli_query($conn, $sql_raw);

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

6 Comments

Hi sir, I've tried that and I'm getting an error "<b>Warning</b>: Invalid argument supplied for foreach()"
you need to remove this line from your jquery : rubricChkbox = rubricChkbox.toString();
from what I see all your checkboxes have the value of 3 : <input class="rubricChkbox" type="checkbox" value="3" /> you should give each a different value
I have fixed that sir. It actually work but the problem about it is that if i select level 3,4,5 it will insert levels 3 and 4 except the level 5.
I don't know about that probably it's an error in the html code
|
0

Yep I've tried it sir,

if(isset($_POST['rubricChkbox'])) {
      $rubric_value = $_POST['rubricChkbox'];
      foreach($rubric_value as $value){

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

        VALUES  ('$value', '1')";

        $success = mysqli_query($conn, $sql_raw);
 }
}

Comments

0
var Hobbies = "";
$(".chkHobbies").each(function (e) {
  if (this.checked) {
    if (Hobbies == "") {
      Hobbies = this.id;
    } else {
      Hobbies = Hobbies + ',' + this.id;
    }
  }
});

2 Comments

you need to have multiple insert statements like this for example:
Please add some explanations to your solution, and not just as comment but also in the answer (edit it)

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.