C5-8.0+ How to execute PHP function and pass json values from add block form?

Permalink
How can I execute a PHP function in the block controller and pass json values to it from an Add Block form, for example, on closing the form I want to save some form data (not from inputs) in the database:
$('#ccm-form-submit-button').on('click', function(e){

There is also a save($args) function, but how do I append my json array to the $args?

Thank you.

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
This works:
form.php
$.ajax({
            dataType: "json",
            type: "post",
            data: {data: dataObjectString},
            url:  '<?php echo $view->action("save", $app->make("token")->generate("ccm-block-form")); ?>'
        })
        .done(function(response) {
            if (response['status'] === "ok") {
                console.log(response['message']);
            }

controller.php:
public function action_save($token = false) 
    {
        echo $this->app->make('helper/json')->encode(['status' => 'ok', 'message' => $this->post('data')], JSON_UNESCAPED_UNICODE);
        exit;
    }

But the token ('message' => $token) is null. Why isn't it passed to the controller?

What am I missing?
mnakalay replied on at Permalink Reply
mnakalay
you are sending data 2 ways here. You are sending your data object with a POST but you are sending your token as a query parameter, so that would be a GET. What's more you're indicating JSON as data type but using query parameters makes it HTML I think.

Anyway, I think this tutorial should help you out:
https://documentation.concrete5.org/developers/working-with-blocks/c...

Maybe this one as well
https://documentation.concrete5.org/developers/working-with-blocks/c...

This website stores cookies on your computer. These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media. To find out more about the cookies we use, see our Privacy Policy.