Barba Barba - 1 year ago 105
jQuery Question

Ajax post to CodeIgniter controller not working

I'm trying to post some data to CodeIgniter controller, but it only shows empty array. I'm also using DataTables. I'm not sure that I'm sending the data the right way. This is my code:


$("#prw").on('click', function(e){

var url = window.location.origin + "/nsl/preview";

url: url,
data: $("input[type='checkbox']:checked").serialize()
console.log("Response", data);


function preview() {
$post = $this->input->post();
return $post;


<table id="offers" class="table table-striped table-bordered" cellspacing="0" width="100%">
<?php foreach($data as $d): ?>
<td><input type="checkbox" id="check" name="myCheckboxes[]" class="toggle" value="<?=$d->oid?>"></td>
<td><input type="text" id="rb" name="myText[]" class="toggle"></td>
<?php endforeach; ?>
<input type="submit" id="prw" class="btn btn-info" value="Preview">

Please I appreciate any help.

Answer Source

Certain things to check :-

  1. Inside the ajax call, mention type : 'post'

  2. On click function, try

    alert( $("input[type='checkbox']:checked").serialize() );

    and check whether you are getting the values as expected?

  3. Open Firebug, Click on Console, execute the click button, see what all values are present under POST tab of the URL inside the console.

  4. Inside the controller

    $post = $this->input->post('myCheckboxes'); print_r($post);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download