Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to dynamically change a view using ajax
  • Greetings guys, I'm trying to develope some ajax functionalities on my site and I'm experiencing some problems while I do so. What I'm willing to do is to change the content of a view dynamically via an Ajax call: function filterlist(){ var data = {
    "order_by": $('#form_order_by').val(),
    "num_items": $('#form_num_items').val()
    }; jQuery.ajax(
    {
    url:'roomsRest/filter_list.php',
    type:'post',
    data:data,
    success:function(data,textstatus, XMLHttpRequest){
    $('#tooltips_container').html('Success');
    },
    error: function(XMLHttpRequest, textStatus, errorThrown)
    {
    $('#tooltips_container').html(errorThrown); }
    }
    ); } As you can see, I'm calling a RoomRest controller, which extendes Controller_Rest. class Controller_RoomsRest extends Controller_Rest {
    public function post_filter_list() { $this->template->content = View::forge('rooms/index'); //I know that this doesn't work. I'm not supposed to forge a view from a Rest controller, but mainly this is what I would like to achieve. } } What I want that controller to do is to renew my $content var on my template with new data. To achieve it, shall I use a Controller that Extends Controller_Template instead?
    So, in few words, how can I make this controller to return the new data and dynamically change it on my template. Thanks!
  • The Controller_Rest doesn't use $this->template (obviously), so that is indeed not going to work. As documented (http://fueldevdocs.exite.eu/general/controllers/rest.html), you need to use the response() method to set your response value(s), and make sure your controller is configured to return html and not something else.
  • Solved and working fine now, thanks a lot for the hint WanWizard! Pasting the response code and the format assignment in case it helps someone in the future: class Controller_RoomsRest extends Controller_Rest {
    public function post_filter_list() { $this->format = 'html';
    $this->response->body = View::forge('rooms/list_iterator', $data);
    } }
  • Better and more future proof is
    class Controller_RoomsRest extends Controller_Rest
    {
        public function post_filter_list()
        {
            $this->format = 'html';
            $this->response(View::forge('rooms/list_iterator', $data));
        }
    }
    

    as $this->response might disappear in the future.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion