The model seems to work the same way as the controller. AJAX displays the results as "null", so I think this is because we need to send the data as json. Any ideas on how to get the data in the correct format and display in the view
View
<button type='button' name='getdata' id='getdata'>Get Data.</button>
<div id='result_table' style="color:white;">
hola amigo
</div>
<script type='text/javascript' language='javascript'>
$('#getdata').click(function(){
$.ajax({
url: 'http://localhost:8888/index.php/trial/getValues',
type:'POST',
dataType: 'json',
error: function(){
$('#result_table').append('<p>goodbye world</p>');
},
success: function(results){
$('#result_table').append('<p>hello world</p>' +results);
alert("Success!");
}
});
});
</script>
controller
function getValues(){
$this->load->model('get_db');
$data['results'] = $this->get_db->getAll();
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($data));
return $data;
}
Model
class Get_db extends CI_Model{
function getAll(){
$query=$this->db->query("SELECT * FROM questions");
return $query->result();
}
}
Ok, so AJAX returns a success warning, however, instead of displaying a table from the database, this is what appears in the div:
Hello world
zero
If I go directly to the URL ( http: //loca.lhost: 8888 / index.php / trial / getValues ), this will be the displayed object:
{
"results": [
{
"qID": "1",
"email": "hello",
"qText": "hello",
"playlistID": "",
"timestamp": "0000-00-00 00:00:00"
},
{
"qID": "2",
"email": "",
"qText": "",
"playlistID": "",
"timestamp": "0000-00-00 00:00:00"
},
}
How to extract this information and display what I want to display?