Laravel:如何为每个按钮创建一个动作的表单

编程入门 行业动态 更新时间:2024-10-15 08:24:11
本文介绍了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.

问题:

  • 如何实施此解决方案?
  • 如果我的解决方案不切实际或愚蠢,什么是更好的解决方案?
  • 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:如何为每个按钮创建一个动作的表单

    本文发布于:2023-10-23 05:20:16,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1519904.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:表单   创建一个   何为   按钮   动作

    发布评论

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

    >www.elefans.com

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