I'm trying to create an ajax search function, where user is to enter the search criteria, then ajax grab those data and send them to php to query the result from the database, then it returns the result along with the pagination link stored in a string.
In the javascript file:
$(document).ready(function(){
$('.song_filter').change(function(){
var url = 'listing/ctrl';
var data {
title: $('#title').val();
author: $('author').val();
}
$.post(url, data)
.success(result) {
var jsonObj = $.parseJSON( result );
output_list(jsonObj); /* fetch out the result */
}
});
});
And in my php listing/ctrl, I have both the query result and the paginated link ready to return. I have no problem returning only the query result on its own, but I don't know how to return both the result and the link.
$result = $this->pagination->get_result();
$page_nav = $this->pagination->page_nav(); // <li class="page">2</li><li class="page">3</li>
echo json_encode($result, JSON_UNESCAPED_UNICODE);
You can append the link to the json object, something like this:
$result = $this->pagination->get_result();
$page_nav = $this->pagination->page_nav();
// create an object to store the result and the link
$json = new stdClass();
$json->link = $page_nav;
$json->data = $result;
echo json_encode($json, JSON_UNESCAPED_UNICODE);
And the in your ajax success
.success(result) {
var jsonObj = $.parseJSON( result );
jsonObj[0].link /* Here comes the link */
jsonObj[0].data;
}