Reputation: 360
I want to get data from database but I cannot pass variable from view to controller using json. Could you please help me to find the mistake.
Here is my view:
<?php
echo $message;
foreach($results as $row)
{
$faq_id = $row->faq_id;
$faq_title = $row->faq_title;
?>
<h3><a href="#" class="faq_title"><?php echo $faq_title; ?></a></h3>
<?php
}
?>
Here is my JS file:
$(".faq_title").click(function(){
var title = $(this).text();
$.ajax({
url: 'faq/get_faq_data',
data: {'title': title},
dataType: 'json',
type: "post",
success: function(data) {
response = jQuery.parseJSON(data);
console.log(response);
}
});
});
Here is my Controller:
public function get_faq_data() {
header('Content-Type: application/json',true);
$this->load->model("model_faq");
$title = $this->input->post('title');
$data["results"] = $this->model_faq->did_get_faq_data($title);
echo json_encode($data["results"]);
}
Here is my model:
public function did_get_faq_data($title){
$this->db->select('*');
$this->db->from('faq');
$this->db->where('faq_title', $title);
$query = $this->db->get('faq');
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
Upvotes: 0
Views: 529
Reputation: 563
var title = $(this).text();
$.post( // you can simply send json from post method of jquery
"<?php echo site_url('faq/get_faq_data') ?>", // try to use full url
{
title : title
},
function( data ) {
console.log( data );
},
"json"
);
Upvotes: 1