0

So I'm not exactly proficient in using codeiginiter but I've been working with it for a while now.

I'm trying to make an SQL query to search for books in the database, but the ajax always returns empty.

I echoed a few dummy strings to make sure that a proper connection is established between the controller and the view, and I made sure the SQL query itself works, but I don't understand why this still happens.

This is the javascript:

    $('#search').on("keyup", function() {
search = $(this).val();
console.log("Search Term: " + search);
$.ajax
({
    type: "POST",
    url: base_url + "vault/quick_search",
    data: {search_term:search},
    dataType: 'json',
    success: function(data)
    {
      console.log("Result: " + data);
    }, 
    error: function(data)
    {
        alert("?");
    }
});
});

This is the controller:

      function quick_search()
    {
       $search = $this->input->post('search_term');
       $result = $this->vault_instance->quick_search($search);
       echo json_encode($result);
    }

This is the model:

      function quick_search($search_term)
 {
   $sql = "SELECT * FROM books WHERE title LIKE '%harr%' OR author LIKE '%harr%'";
    $query = $this->db->query($sql);
    return $query->result_array;
 }

NOTE: sorry for the poor editing, this is my first time asking a question. Also, in the model I used "harr" as a test, I understand it's not the search term itself, it still returns empty

7
  • But you did good editing of your code. But wait for the hints. Commented Feb 26, 2016 at 19:49
  • Welcome! Are you developing on your local machine? Some browsers have security measures in place that make Ajax tricky. Are there any errors/warnings in your browser's dev console? Commented Feb 26, 2016 at 19:51
  • Did you check if you see any errors in your console Commented Feb 26, 2016 at 19:51
  • I think u have issue here return $query->result_array; this should be return $query->result_array(); Commented Feb 26, 2016 at 19:59
  • Yes, I'm working on my local machine. There aren't really any warnings or errors. I've done many other AJAX calls; they worked just fine. This one isn't, however. Commented Feb 26, 2016 at 20:00

3 Answers 3

2

You need to change this line:

return $query->result_array; 

With:

return $query->result_array();

Becuase result_array() is a function which return you result into array format.

Sign up to request clarification or add additional context in comments.

Comments

0

What is the collation you using for your database column? If not utf8_general_ci try to change to it.

Comments

-1

Not sure if this works, but... try to change the datatype in your $.ajax call to jsonp (instead of json).

3 Comments

That will not change anything
The AJAX returns an error now. Is there anything else I need to modify for jsonp to work?
@Sushanth jsonp helps if you're not getting AJAX data returned due to the Same-Domain policy restriction.

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.