skip to Main Content

I have multiple check boxes 32 to be exact.

<input type="checkbox" class="custom-control-input" id="m1"> -> <input type="checkbox" class="custom-control-input" id="m32">

On my Javascript I am checking each check box if it is checked or not the I will assign a value 1 for checked and 0 for unchecked.

//I got 32 of these
if (document.getElementById("m1").checked){
    m1 = '1';
}
else{
    m1 = '0';
}

Then I am sending the data using Ajax.

$.ajax({
    url: 'submit-user-details.php',
    method: 'POST',
    dataType: 'TEXT',
    data: {firstname : firstname, lastname : lastname, middlename : middlename, usercd : usercd, password : password, usergrp : usergrp, userstatus : userstatus, branch : branch, m1 : m1, m2 : m2, m3 : m3, m4 : m4, m5 : m5, m6 : m6, m7 : m7, m8 : m8, m9 : m9, m10 : m10, m11 : m11, m12 : m12, m13 : m13, m14 : m14, m15 : m15, m16 : m16, m17 : m17, m18 : m18, m19 : m19, m20 : m20, m21 : m21, m22 : m22, m23 : m23, m24 : m24, m25 : m25, m26 : m26, m27 : m27, m28 : m28, m29 : m29, m30 : m30, m31 : m31, m32 : m32},
    success: function(response) {
        $('#confirmModal').modal('hide');
});

Then in my PHP, each value have their own Insert Query (Note: please ignore my query I know that this is not secured but I am currently using an old version of XAMPP there is no PDO support for Oracle).

//I got 32 of these
$modulerightsql = oci_parse($conn, "INSERT INTO ASUSERMODULERIGHTS(USERCD, SWITCH, MODULEID) VALUES('". $usercd ."', '". $m32 ."', '32')");
oci_execute($modulerightsql);

Is there a better way to save the value of my check boxes better?

2

Answers


  1. Use array notation for checkboxes names:

    <input type="checkbox" name = "m[]" class="custom-control-input" id="m1">
    <input type="checkbox" name = "m[]" class="custom-control-input" id="m32">
    

    In this case $_POST[‘m’] is going to be an array of checked values.

    Login or Signup to reply.
  2. Send this multipleValue variable to ajax File. you get value.
    example : m1,m2,m3

    $(document).ready(function(){
      $("#submit").click(function(){
        
       var multipleValue = [];
        $(':checkbox:checked').each(function(i){
          multipleValue[i] = $(this).val();
        });
        console.log(multipleValue);
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <input type="checkbox" class="custom-control-input" id="m1" name="m[]" value="m1"> M1
    <input type="checkbox" class="custom-control-input" id="m1" name="m[]" value="m2"> M2
    <input type="checkbox" class="custom-control-input" id="m1" name="m[]" value="m3"> M3
    <input type="checkbox" class="custom-control-input" id="m32" name="m[]" value="m32"> M32
    <input type="submit" id="submit">
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search