lundi 6 février 2017

insert checkbox array to database

i have multiple checkboxes that i want to insert to my database.

i have this code where im trying to take all the checkbox values via array, but what im inserting on the database are blank, perhaps im taking the array the wrong way

checkboxes

 <label class="checkbox-inline"><input type="checkbox" class="modalday" name="modalday[]" value="M">Monday
                          &nbsp;&nbsp;</label>
                        </div>
                        <div class="checkbox">
                          <label class="checkbox-inline"><input type="checkbox" class="modalday" name="modalday[]" value="T">Tuesday
                            &nbsp;&nbsp;</label> 
                          </div>
                          <div class="checkbox">
                            <label class="checkbox-inline"><input type="checkbox" class="modalday" name="modalday[]" value="W">Wednesday
                              &nbsp;&nbsp;</label>
                            </div>
                            <div class="checkbox">
                              <label class="checkbox-inline"><input type="checkbox" class="modalday" name="modalday[]" value="Th">Thursday
                                &nbsp;&nbsp;</label>
                              </div>
                              <div class="checkbox">
                                <label class="checkbox-inline"><input type="checkbox" class="modalday" name="modalday[]" value="F">Friday
                                  &nbsp;&nbsp;</label>

this is how i take the data

 function saveData(){
                      var modsubj = $('#modalsubject').val();
                      var modsect = $('#modalsection').val();
                      var modstart = $('#modalstarttime').val();
                      var modend = $('#modalendtime').val();
                      var moduser = $('#userID').val();
                      var modday = new Array();

                      $('.modalday input:checked').each(function() {
                        modday.push($(this).attr('name'));
                      });

                      $.ajax({
                        type: "POST",
                        url: "modal.funcs.php?p=add",
                        data: "subj="+modsubj+"&sect="+modsect+"&day="+modday+"&start="+modstart+"&end="+modend+"&user="+moduser
                      });

                    } 

and this is where i process the data taken from ajax

$page = isset($_GET['p'])?$_GET['p']:'';
if($page=='add'){

  $subj = $_POST['subj'];
  $sect = $_POST['sect']; 
  $day = $_POST['day'];
  $strTime = $_POST['start'];
  $endTime = $_POST['end'];
  $user_id = $_POST['user'];



  $auth_user->createSchedule($subj,$sect,$day,$strTime,$endTime,$user_id);
  $schedRow = $auth_user->readSchedule();

} else if ($page=='edit') {

}

i think whats wrong is how i take the data from the array, but im not sure how to do it correctly, please help




Aucun commentaire:

Enregistrer un commentaire