0

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;
    }
   }    
2
  • try removing () from $.ajax data parameter. Try this: data: {'title': title}, Commented Jun 1, 2014 at 4:49
  • I treid in that way. The problems till exists. Commented Jun 1, 2014 at 5:06

1 Answer 1

1
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"
);
Sign up to request clarification or add additional context in comments.

2 Comments

I tried to use full url. It did not work. I tried to use post method as well. It did not work.
header('Content-Type: application/json',true); there is not need of header

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.