Ragnar Ragnar - 2 months ago 6
Ajax Question

Codeigniter null values ajax request

, i'm triying to get some values from database on an ajax request in codeigniter...but json object returns null ([]) when I put console.log...I need help pls !!

JAVASCRIPT

function list_president() {
var section = "1";
$.post(baseurl + 'votos/load_politic', section,
function(data) {
console.log(data);
});
}


CONTROLLER

public function load_politic()
{
if ($this->input->is_ajax_request()) {

$section = $this->input->post('section');
$result = $this->politic->get_president($section);

echo json_encode($result);
}
}


MODEL

public function get_president($section){


$this->db->select("p.POLITIC_NAME, p.POLITIC_LASTNAME, p.POLITIC_SIDE, p.POLITIC_CHARGE");
$this->db->from("politics p");
$this->db->where("SECTION_ID",$section);

$result= $this->db->get();

return $result->result();
}


Thanks for help!!

Answer

You aren't sending key/value pair to server...just a value.

So there is no $_POST['section'] which is basically what $this->input->post('section'); is

Try changing

 var section = "1";

To

 var section = {section: "1"};

You also aren't validating what is sent or checking if $result returns anything