AJAX with block add/edit, specifically controller action references

Permalink 1 user found helpful
(edited this post title to be more general, seems no doc on ajax out using controller actions from outside of view.php)

$this->action('whatever');


gives null when used in add/edit.php (obviously where whatever is a function properly declared in controller.php).

Any idea why that would be? Trying to use it within a php script included by a block's add.php.

The action function seems properly specified in the controller.php.

Thanks!

dvodvo
 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
You can't use $this->action() in add/edit pages (not sure why, that's just the way it is). When I have AJAX calls from add/edit forms, I instead create a file in the "Tools" directory.

Is this a block in your site's "blocks" directory or are you creating it in a package? If the former, create a new file called "whatever.php" and put it in your site's "tools" directory. If the latter, create a new directory called "tools" inside your package's directory and put the new "whatever.php" file in there.

Then just echo out whatever you want as an AJAX response (e.g. a JSON string, a chunk of HTML, a status code, whatever), and call this via a jquery ajax request from your add/edit form.

To get the address of this file, use this (if the block is just in your site's blocks directory):
$th = Loader::helper('concrete/urls');
echo $th->getToolsURL('whatever'));


Or use this (if the block is in a package):
$th = Loader::helper('concrete/urls');
echo $th->getToolsURL('whatever', 'my_package_name'));


Note that in both cases, you're passing a string to the "getToolsURL" function that is the name of your tools file WITHOUT the ".php" extension.

Hope that helps!

-Jordan
miron replied on at Permalink Reply
Thanks Jordan, you're my personal hero :)

I spent almost 5 hours trying to get the ajax calls getting to work from the add and edit pages from a block.

It's a bit pitty that you have to create an additional file in the tools folder to do ajax calls (from add and edit pages), this should be fixed in the upcoming releases...
jordanlev replied on at Permalink Reply
jordanlev
My pleasure! If you mark my response as the "best answer" (or whatever it's called), I'll get bonus karma points :)

And I agree that it's too bad you have to create a separate file (kind of goes against the whole point of using MVC and having a controller). I'm not sure if this is just an oversight or if there's something in the architecture of C5 that makes it really difficult, but you should submit it as a feature request -http://www.concrete5.org/private/features/...

-Jordan
ScottC replied on at Permalink Reply
ScottC
you have to prepend the method call with action_ in your controller's method/action for this to work so for example:

method="post" action="<?=$this->action('form_process_payment'); ?>">


runs in:

function action_form_process_payment(){


when in a block context, posting to tools is certainly fine, but that is another option :)

Some of the wrapper functions in $this->post( etc are useful, though even a request to tools runs through dispatcher so all of your stuff is available concrete5 wise.

Just another opinion, not in it for the karma :)
jordanlev replied on at Permalink Reply
jordanlev
Scott,
The OP is talking about calling a method from the add/edit form, not the block's view. $this->action() doesn't work in this context (not sure why, but if you try it out yourself you'll see that nothing is outputted by $this->action() calls).
ScottC replied on at Permalink Reply
ScottC
you're right, glazed over that.

One could load up the block and grab and instance of the controller(and access its methods) but at that point i think the path of least resistance is in using the tools approach, which it seems we both like :)