如何使用SlimFramework将多个表单发布到相同的路径?(how to post multiple forms to same route using SlimFramework?)

编程入门 行业动态 更新时间:2024-10-25 02:24:01
如何使用SlimFramework将多个表单发布到相同的路径?(how to post multiple forms to same route using SlimFramework?)

我有以下视图(main.php),其中包含发布到同一路由的两个单独的表单:

<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> // form data </form>

以下是呈现该视图并发布表单数据的路由逻辑:

$app->get('/main', function() use ($app) { $app->render('main.php'); })->name('main'); $app->post('/main', function() use ($app) { // do something })->name('main.post');

现在在我的$app->post方法中,我想区分提交的表单和相应的单独逻辑,如下所示:

$app->post('/main', function() use ($app) { if(first form was submitted) { // do something } else if(second form was submitted){ // do something else } })->name('main.post');

我想要完成的工作是否需要利用中间件? 如果没有,我需要实现什么逻辑来完成这个看似简单的任务

I have the following view (main.php) that contains two separate forms that post to the same route:

<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> // form data </form>

Here is the route logic that renders that view and also post the form data:

$app->get('/main', function() use ($app) { $app->render('main.php'); })->name('main'); $app->post('/main', function() use ($app) { // do something })->name('main.post');

Now within my $app->post method, I would like to differentiate between which form was submitted and do separate logic accordingly, like so:

$app->post('/main', function() use ($app) { if(first form was submitted) { // do something } else if(second form was submitted){ // do something else } })->name('main.post');

Does what I am trying to accomplish need to utilize Middleware? if not, what logic do i need to implement to accomplish this seemingly simple task?

最满意答案

您可以在表单中包含一个隐藏字段,指示提交的表单。

<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="first_form"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="second_form"> // form data </form>

然后在您的路线中检查which输入的值。

$app->post('/main', function() use ($app) { $which = $app->request()->post('which') if ('first_form' === $which) { // do something } else if ('second_form' === $which){ // do something else } })->name('main.post');

You could include an hidden field in your form which indicates which form was submitted.

<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="first_form"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="second_form"> // form data </form>

Then in your route check the value of which input.

$app->post('/main', function() use ($app) { $which = $app->request()->post('which') if ('first_form' === $which) { // do something } else if ('second_form' === $which){ // do something else } })->name('main.post');

更多推荐

本文发布于:2023-08-02 19:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   表单   如何使用   路径   SlimFramework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!