4

I am using CodeIgniter as my web framework and from controller, I am using $this->load->view('Apartments_Hotels', $data to load data for the first time. But now I need to reload the page with new data using an ajax function in the view.

Ajax Function in the View

$(document).ready(function() {
    $(".filter_checkbox").click(function(){
        var filter = [];
        $.each($("input[name='filter']:checked"), function(){
            filter.push($(this).val());
        });
        $.ajax({
            async: false,
            type: 'POST',
            data: {
                filter: filter
            },
            url: "<?php echo base_url(); ?>index.php/ListingsController/showFilterListings",
            success: function (response) {

            }
        });
    });
});

Controller Function

public function showFilterListings(){echo $this->load->view('Apartments_Hotels', $data, TRUE);

}

But this is not working. Could anyone please help me with loading view again and again using ajax function

2
  • Invoke your PHP page in a Chrome browser, with F12 (Chrome developer tools) active. You can add a debugger; statement to "$(document).ready()" to make it easier to step through the browser-side JS code. Q: Does your JS look OK in Chrome? Commented Mar 3, 2019 at 19:24
  • I tried to alert the response and it shows the whole page I need to reload which includes all HTML and HEAD tags. But I have no idea how to show that page Commented Mar 3, 2019 at 19:37

1 Answer 1

2

From your original post, I didn't understand what "wasn't working".

You already know how to display your view (by invoking the controller). You already know how to use Ajax (to get information from the server), and how to use jQuery (to manipulate the DOM rendered by your browser for the view).

The problem is that you don't want to refresh the entire page. Which seems to be exactly what you're doing, with your current Ajax call.

The solution is:

  1. For the initial display, have your CI controller return a view - exactly like you're already doing.

  2. The view will have a "$(document).ready()" action handler, which will make an Ajax call - exactly like you're already doing.

  3. Add a <div id="xyz"...> tag to your view, in order to receive "refresh data" from the server.

  4. Your Ajax call will invoke a different URL - a URL that returns JSON data - the data you want to "refresh".

  5. Your Ajax handler will a) parse out the JSON from the response, and then b) Use it to update the <div> in your DOM.

Here are some examples:

Using AJAX to reload only a Div

Let jQuery AJAX Change This Text

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

1 Comment

Beautiful - glad to hear it :)

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.