How to insert the value on my checkbox to differen

2019-08-25 07:29发布

问题:

This my current output -

And my Ajax code is like this -

There's no error. but it can't pass through my database

MY CONTROLLER

public function insertNewRole(){
    $basic_data = array();

    $basic_data = array(
        'accs_trans_sec' => $_POST['select_access1'],
        'accs_acctng_sec' => $_POST['select_access2'],
        'accs_admin_sec' => $_POST['select_access3'],
        'accs_dashboard_sec' => $_POST['select_access4'],
        'accs_reports_sec' => $_POST['select_access5']
    );
    $this->RoleModel->saveRole($basic_data);
}

回答1:

I just see the issue here. I see that datastring variable is an string that each value is separated by , that you just use to post on your controller right? So it will be $_POST['data'] on your controller.

Then try to use explode() like this on your controller:

$parseddata = explode(',',$_POST['data']);

Let's continue to your code:

   public function insertNewRole(){

    $parseddata = explode(',',$_POST['data']);

     $basic_data = array();

    $basic_data = array(
        'accs_trans_sec' => $parseddata[0], //The value should be 1
        'accs_acctng_sec' => $parseddata[1],//The value should be 0
        'accs_admin_sec' => $parseddata[2], //The value should be 1
        'accs_dashboard_sec' => $parseddata[3], //The value should be 0
        'accs_reports_sec' => $parseddata[4] //The value should be 1
    );
    $result = $this->RoleModel->saveRole($basic_data);

     if($result == true){
          echo ("Successfully inserted!");
     }else{
           echo ("Problem!");
      }
}

Let's see if you can get through this.

EDIT: Based on your previous post, your model has a loop on it, I modify it and should like this:

public function saveRole($basic_data)
   {
    $this->db->insert('roles_global_access', $basic_data);

    return ($this->db->affected_rows() != 1) ? false : true;
   }