0

Can you guys tell me what i'm doing wrong here in my code? I tried to post the data inside these checkboxes to database

here is the view.php

(more code)
<div class="form-group">
          <label class="control-label col-md-3">Infection</label>
          <div class="col-md-9">
            <input type="checkbox" name="infectionType[]" value="vap"> VAP
            <input type="checkbox" name="infectionType[]" value="hap"> HAP
            <input type="checkbox" name="infectionType[]" value="isk"> ISK
            <input type="checkbox" name="infectionType[]" value="iad"> IAD
          </div>
        </div>

and this is my controller.php

public function insert_surveilance(){
    $data=array(
        (more code)
        'vap' => $this->input->post('vap'),
        'hap' => $this->input->post('hap'),
        'isk' => $this->input->post('isk'),
        'iad' => $this->input->post('iad'),
        (more code)
    );
    $data[]=
        $insert = $this->surveilance_model->insert_surveilance($data);
        echo json_encode(array("status"=>TRUE));
}

then this is my model.php

public function insert_surveilance($data){
    $this->db->insert($this->table, $data);
    return $this->db->insert_id();
}

this is the save function ... function save(){

    var url;
    if(save_method == 'add'){
      url="<?php echo site_url('surveilance/insert_surveilance')?>";
    } else {
      url="<?php echo site_url('surveilance/edit_surveilance')?>";
    }
    $.ajax({
        url : url,
        type: "POST",
        data: $('#form').serialize(),
        dataType: "JSON",
        success: function(data)
        {
           //if success close modal and reload ajax table
           $('#modal_form').modal('hide');
          location.reload();// for reload a page
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Failed adding data');
        }
    });
  }
7
  • Have you check under XHR if your url is successfully called. Commented Feb 21, 2019 at 6:37
  • it did successfully called, but the value is null. A Database Error Occurred Error Number: 1048 Column '....' cannot be null INSERT INTO surveilance (...,vap, hap, isk, iad,...) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) Filename: D:/xampp/htdocs/ppi-daily/system/database/DB_driver.php Line Number: 691 Commented Feb 21, 2019 at 6:51
  • But when i change the type from checkbox to text, it did saved to database Commented Feb 21, 2019 at 6:54
  • The variables you used on your controller $data[]= and $insert = you can remove it since you are not using it. Commented Feb 21, 2019 at 6:54
  • I see it now. You must use 'vap' => $this->input->post('infectiontype[]') instead of 'vap' => $this->input->post('vap') Commented Feb 21, 2019 at 6:56

1 Answer 1

1

First of all var_dump($this->input->post()) in your controller, if it shows post data then try replacing below code.

public function insert_surveilance(){
    $request_params = $this->input->post('infectionType');
    $data=array(
        (more code)
        'vap' => $request_params[0],
        'hap' => $request_params[1],
        'isk' => $request_params[2],
        'iad' => $request_params[3],
        (more code)
    );
    $data[]=
        $insert = $this->surveilance_model->insert_surveilance($data);
        echo json_encode(array("status"=>TRUE));
}
Sign up to request clarification or add additional context in comments.

1 Comment

i did var_dump, it return = array(0) { }. What does it mean? The controller doesn't recognize form, does 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.