Passing AJAX data to area
Permalink 1 user found helpful
Hi fine C5 folks,
I've been trying to get fancy a dynamically load some data to an area using a block that handles an AJAX request. If I correctly pull data in the controller, I'm unable to pass them to my page. So far I can only print them from within my controller method.
The block is requested in the Product area. The info to be passed is gotten by the getCollection() controller method.
Since it's an AJAX call, I've tried buffering the data before outputting it but it didn't work. I also read several blog posts in the forum such as this one (http://www.concrete5.org/community/forums/customizing_c5/passing-data-from-block-controller-to-page-template/) but I'm not sure it applies to my case.
Let me show you what my code looks like.
The controller:
The page where I'd like to used the info, from the block set in an area.
Maybe it's my code architecture that is plain wrong. Help greatly appreciated!
Thank you
I've been trying to get fancy a dynamically load some data to an area using a block that handles an AJAX request. If I correctly pull data in the controller, I'm unable to pass them to my page. So far I can only print them from within my controller method.
The block is requested in the Product area. The info to be passed is gotten by the getCollection() controller method.
Since it's an AJAX call, I've tried buffering the data before outputting it but it didn't work. I also read several blog posts in the forum such as this one (http://www.concrete5.org/community/forums/customizing_c5/passing-data-from-block-controller-to-page-template/) but I'm not sure it applies to my case.
Let me show you what my code looks like.
The controller:
class Products extends Controller { public function getCollection() { // .... getting some dB data $this->set('products',$products); // doesn't work (most probably because of the asynchronous nature of AJAX) print_r($products); // displays pulled data in the area but I can't manipulate the array further on } }
The page where I'd like to used the info, from the block set in an area.
$a = new Area('Products'); ob_start(); $a->display($c); $ret = ob_end_clean(); echo $ret; // returns '1' (?) but not the expected array
Maybe it's my code architecture that is plain wrong. Help greatly appreciated!
Thank you
Here I am with some more info (after struggling a couple of hours more).
I can now pass correctly my data from the controller to the view buffer, except that what is passed is the whole AJAX block.
Is there a way to pass only the result and not the view itself?
The custom block that makes the AJAX call and that is output in a view area:
Thank you folks
I can now pass correctly my data from the controller to the view buffer, except that what is passed is the whole AJAX block.
Is there a way to pass only the result and not the view itself?
The custom block that makes the AJAX call and that is output in a view area:
<script type="text/javascript"> $(document).ready(function(){ $.ajax({ url: "<?php echo URL::to($data['route'] . '/'. $data['pageID']);?>", success: function(response) { $('#ajax-content').html(response); } }); }); </script> <div id="ajax-content"></div>
Thank you folks
I know this isn't the way but I can't find anything more about the subject.
So if you have an idea, feel free to share it.
Thanks again!