• 0

Codeigniter pagination with jQuery


Question

Hey guys, I have been stuck on this problem for days and am about the crack... I have no idea what im doing wrong or where i should look for help.

I have been using the following ajax pagination library.

The search page allows users to filter the results by a number of categories.

This is the search view:

<form method="post" id="search_form" name="search_form">
 <div id='sidebar_wrapper'>
  <div id='sidebar'>
   <div class="slider_header">
    Country
   </div>
   <div class="slider_content">
    <select id="country_dropdown" name="country_dropdown">
        <option value='1' >Country 1</option>
        <option value='2' >Country 2</option>
    </select>
   </div>
   <div class="slider_header">
    Region
   </div>
   <div class="slider_content">
    <select id='region_dropdown' name='region_dropdown'>
        <option value='1' >Region 1</option>
        <option value='2' >Region 2</option>
    </select>
   </div>
   <input id='profile_search_button' type='button' value='Search'></input>
  </div>
  <div id='sidebar_content'>
   <div id='content_header'>
    Search
   <div id='search_results'></div>
  </div>
  <div id='sidebar_footer'></div>
 </div>
</form>

So when the "profile_search_button" is clicked the following jQuery code is run:

$("#profile_search_button").click(function()
{
    $('#search_results').hide();

    $.post("http://localhost/index.php/search/search_database", { "serialised_form" : serialize_form() },
    function(data)
    {
        $('#search_results').html(data);
        $('#search_results').slideDown('slow');
    });
 });

function serialize_form()
{
    return $('#search_form').serialize();
}

That function calls the following PHP function from the search controller:

public function search_database()
{
    echo $this->input->post('serialised_form');

    $this->load->library('Jquery_pagination');

    $config['base_url'] = site_url('search/search_database');
    $config['total_rows'] = 100;
    $config['per_page'] = '10';
    $config['div'] = '#search_results';
    $config['additional_param']  = 'serialize_form()';

    $this->jquery_pagination->initialize($config);
    echo $this->jquery_pagination->create_links();
}

Ok, so I can pass the form data to the PHP function the first time it is called - I can echo out the serialised data (I'll look into parsing that information at a later stage). The problem seems to be when a pagination link is clicked. The same PHP search function is called again but the form data is not sent with to the page...

The author also says that I should add the following to the config file:

$config['additional_param']  = 'serialize_form()';

Ive done that, and added the necessary javascript to the view to serialize the form data

<script type="text/javascript">
    function serialize_form() {
        return $('#search_form').serialize();
    }
</script>

Apparently I should "receive filtering datas as POST in your pagination function".

But how do i access this form data... Ive tried to read the POST and GET values but they wont work.

This is the example i have been working on: http://tohin.wordpress.com/2008/10/07/codeigniter-ajax-pagination-exampleguideline

Doesn't seem to be much more info out there... can anyone help?

Hope this makes sense...

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Got it working guys. Just a few silly mistakes.

Had to serialise the form data instead of sending it as a JSON object

$("#profile_search_button").click(function()
{
    $('#search_results').hide();

    $.post("http://localhost/index.php/search/search_database",  serialize_form(),
    function(data)
    {
        $('#search_results').html(data);
        $('#search_results').slideDown('slow');
    });
 });

And turns out that the 'additional_param' was working after all.

Uhh... time for bed I think :laugh:

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.