AJAX post from View to Controller in Block - how does the route controller work?
Permalink<?php echo $view->action("do_something");?>
I'm assuming it takes the second parameter, uses this to find the block id, and then uses the first parameter to know which method to call. I also assume this route is handled differently from the GET routes, else a method called sale() and a page called sale would conflict. I had a little look in src/Routing and the bootstrap files, but I couldn't find anything that could help me understand why the ajax request below is not working.
For clarity, it does not work with the post method on a standard php form either
<form action="<?php echo $view->action("do_something");?>" method="post"> <input name="item" type="text"/> <button type="submit">submit</button> </form>
If anyone can help me understand this better, (or even just point out my typo!) that would be great. Thanks
controller.php
public function do_something($bID = false) { echo 'this is the response'; }
view.php
$.ajax({ type : 'POST', // define the type of HTTP verb we want to use (POST for our form) url : '<?php echo $view->action("do_something");?>', data : formData, // our data object dataType : 'json', // what type of data do we expect back from the server encode : true }) .done(function(data) { console.log(data); })
public function action_do_something($bID = false)
{
echo 'this is the response';
exit;
}
in view.php you can use $this->action("do_something")