【问题标题】:Formaction posting on button [LARAVEL]按钮 [LARAVEL] 上的 Formaction 发布
【发布时间】:2018-04-23 20:01:38
【问题描述】:

我目前在提交按钮上直接发布方法时遇到问题。我需要做的是将两个提交按钮放在同一个表单中。我搜索了一下,找到了html“formaction”方法。问题是在一个页面上,它工作得很好,但在另一个页面上却不行。

所以,下面是我的有效代码:

<form action="" method="POST">
  {{ csrf_field() }}
  <input type="hidden" name="id" value="$id">
  <button type="submit" formaction="{{route('remove')}}">
      <i class="fa fa-check-circle" aria-hidden="true"></i>
  </button>
</form>

而那个不是:

<form method="POST">
  {{csrf_field()}}
  <input type="hidden" name="id" value="{{$q->id}}">
  <button type="button" formaction="{{route('remove')}}">
    <i class="fa fa-trash" aria-hidden="true" style="font-size: 20px; color: red;"></i>
    </button>
</form>

有人知道为什么会这样吗?我现在正在寻找一段时间,但我发现了死胡同。

【问题讨论】:

  • 您不能对同一个表单进行两个操作。创建两个不同的表单或使用 javascript 将表单提交到基于按钮的特定 URL。或者将它们提交到同一个页面,但为每个提交按钮使用不同的值。
  • 好的。谢啦。我在 w3schools 中查找过,有人说它应该以这种方式工作,但我会调整我的代码。
  • 实际上,我收回了这一点。根据developer.mozilla.org/en-US/docs/Web/HTML/Element/button,它会覆盖表单的操作。但是,它仅在 HTML5 中可用。

标签: php html laravel laravel-4


【解决方案1】:

建议

为每个按钮添加name="",并使用$_POST["name_of_submit"] 处理表单提交。例子

//1st button
<button type="submit" name="submit1" formaction="{{route('remove')}}">

//2nd button
<button type="submit" name="submit2" formaction="{{route('remove')}}">

if (isset($_POST['submit1'])){
   //do something
 }
if (isset($_POST['submit2'])){
   //do something else
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2023-03-18
    相关资源
    最近更新 更多