I
I
Igor Vasiliev2017-07-29 12:50:29
Yii
Igor Vasiliev, 2017-07-29 12:50:29

How to submit two forms on one Yii2 action?

Hello.
---
My background: about six years ago, when I didn't know about Yii yet, there were such tasks, to send two or three forms from one page, and it was done simply, by name.
Roughly speaking, if the Then button in the conditions you write:
<input type="submit" name="send"/>

<?php
if(isset($_POST['send'])){
// то происходит это
}
?>

If another form has a button: Then:
<input type="submit" name="rezult"/>
<?php
if(isset($_POST['rezult'])){
// то происходит это
}
?>

according to the logic of things: Yii2
...
if(isset(Yii::$app->request->post('send')){}
...

if button
<?= Html::submitButton('отправить', ['name' => 'send', 'class' => 'btn btn-primary']) ?>

But that's how I did it, so I'm writing here.
The thing is, for some reason I don't find any functionality that would submit forms by their name. Everything is sent at once, regardless of the fact that the forms are different, where is the logic?
Question: how to implement in the action sending forms by their names in submits?
PS I clarify, with the help of forms, I am doing a check and not sending data to the database, that is, post information is transmitted via Html::beginForm();.
Two forms are installed there, it is necessary, and this is not discussed, just as the question of why air conditioning is in the car is not discussed. Also here, for convenience, first one form works, which sends something, the other just confirms, and all this happens on one page. Functional specifics.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Igor Vasiliev, 2017-07-29
@Isolution666

The solution is very simple:

...
$string = Yii::$app->request->post('send');
   if (isset($string)) {
         ...
   }
...

D
Dmitry, 2017-07-29
@slo_nik

Good afternoon.
Look in the documentation, there is such a method as formName(), if I'm not mistaken.
Check what this method returns, write code based on it.

A
Arman, 2017-07-29
@Arik

but the form is one, but the action is not one? What prevents this form class from implementing two methods that are launched from button[name=btn][value=send] or button[name=btn][value=result]?

N
Nikita, 2017-07-29
@bitver

All this is already provided, just make 3 forms and which one you send - this one and accept it on the server. The point is that forms are submitted with the [modelName] attribute

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question