Laravel:如何为每个按钮创建一个动作的表单 [英] Laravel: How to make a form with one action for each button

查看:107
本文介绍了Laravel:如何为每个按钮创建一个动作的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格:

<form action="caption/{{$caption->id}}" method="delete">
    <p value='{{$caption->id}}'>{{$caption->content}}</p>
    <button type="submit">Delete caption</button>
    <button type="submit" formaction="<SEND A PUT TO CAPTION/CAPTION_ID>">Accept caption</button>
</form> 

此表格可以处理我网站上图片的未经批准的标题的删除或批准.我已经设置好表单,以便在按下Delete button时将DELETE发送到caption/caption_id.

This form handles the deletion or approval of unapproved captions for pictures on my website. I have set up the form so that a DELETE is sent to caption/caption_id if Delete button is pressed.

现在,按Accept caption按钮后,我希望将$caption->approved字段设置为1(默认为0).我已经考虑过如何做到这一点,而我提出的解决方案(我不知道如何实现)是这样的:

Now, upon pressing the Accept caption-button, I want the $caption->approved field to be set to 1 (it is 0 by default). I have thought of how to do this, and my proposed solution (that I don't know how to implement) is this:

按下接受按钮时,使网站向caption/caption_id发送POST请求.然后,POST由路由处理,该路由将其发送到我需要执行的操作的控制器.

When pressing the accept, make the website send a POST request to caption/caption_id. The POST is then handled by the routing which sends it to a controller where I do what I need to do.

问题:

  1. 如何实施此解决方案?
  2. 如果我的解决方案不切实际或愚蠢,什么是更好的解决方案?

Thx

推荐答案

是否可以不使用两个按钮发送请求,每个按钮的形式均带有所需的目标操作和方法?

Could you not send the request using two buttons, each in a form with the desired target action and method?

{{ $caption->content }}
<form action="caption/{{ $caption->id }}" method="POST">
    <button type="submit">Approve</button>
</form>
<form action="caption/{{ $caption->id }}" method="POST">
    <input name="_method" type="hidden" value="DELETE"/>
    <button type="submit">Reject</button>
</form>

这种方法意味着您不需要使用JavaScript.

This approach means you wouldn't need to use JavaScript.

这篇关于Laravel:如何为每个按钮创建一个动作的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆