AJAX with block add/edit, specifically controller action references1 user found helpful
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.
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!
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...
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/...
method="post" action="<?=$this->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 :)
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).
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 :)