Multiple Form Post Method in Single Controller Action

I have a single page that i hope can save data from 2 form post, exactly subscribes and contacts, and this is my controller:

        $subscribe = $this->Subscribes->newEmptyEntity();
        if ($this->request->is('post')) {
            $subscribe = $this->Subscribes->patchEntity($subscribe, $this->request->getData());
            if ($this->Subscribes->save($subscribe)) {
                $this->Flash->success(__('You have new Message'));
            }
            $this->Flash->error(__('Sorry'));
        }
        $this->set(compact('subscribe'));

        $contact = $this->Contacts->newEmptyEntity();
        if ($this->request->is('post')) {
            $contact = $this->Contacts->patchEntity($contact, $this->request->getData());
            if ($this->Contacts->save($contact)) {
                $this->Flash->success(__('You have new Message'));
            }
            $this->Flash->error(__('Sorry'));
        }
        $this->set(compact('contact'));

my question is how to separate the 2 form post method to get action
thankyou

Will people fill out both sections and you want to save all the data at once, or will they will out one or the other and you need to save only that?

there are two form action on one page, but I want each action just running itself
this my index.ctp

You should have separate endpoints for those two forms to submit to, each of which has half of what you’ve shown from your controller.

You could put a hidden value in each form and determine which was posted by checking the value. But I agree with @Zuluru, it will be better to have two endpoints.

You could put names on the buttons too, and check that, but these are two fundamentally different operations, and thus should be handled by completely separate functions.

any references how to use that endpoints, I’am still newbie at this

When we say ‘endpoint’ we are referring to the controller/action that will receive your post data.

You should have a different action that handles each form.

This section describes how to set the action that will handle your form’s post data.

https://book.cakephp.org/3/en/views/helpers/form.html#setting-a-url-for-the-form

In your case you are working with Subscribes data and Contacts data. Typically, these two tasks would be performed in two different Controllers.

Write a SubscribersController action to handle one forms data and a ContactsController action for the other. Make sure the forms point to the proper new endpoints and you’re good to go.

In the case that you’re creating the same type of record for both, and just taking more data from contacts than subscribers, then the functions could reasonably both be in the same controller, but definitely different functions.

Oh I see, so I have to set the form action is direct to controller each table subscribers and contacts
thank you so much

The form action has a default that’s right in most cases, but can be overridden when required.

okay, thank you so much