【问题标题】:POST method of <form> is not working inside @foreach on blade file in laravel 5.6<form> 的 POST 方法在 laravel 5.6 中刀片文件的 @foreach 内不起作用
【发布时间】:2018-12-14 21:54:39
【问题描述】:

在@foreach 中使用表单后按钮不起作用。关于如何在@foreach 循环中使用表单有什么建议吗?

是否可以在 laravel 刀片文件的循环中使用表单?

这是我的 laravel 刀片文件的代码。我在 foreach 中使用了表单来获取账面金额的数据。有关如何解决此问题的任何建议?

@extends('layouts.app')

@section('content')
<div class="container">
  <h1>Items On Cart:</h4>
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif
                        <table style="border-collapse: collapse;width: 100%;">
                        <tr style="border: 1px solid #dddddd;text-align: left;padding: 8px;font-size:20px;color: gray">
                            <th>Book Name</th>
                            <th>Author Name</th>
                            <th>Book Amount</th>
                            <th>Price</th>
                            <th>Action</th>
                            <th>Action</th>
                        </tr>
                        @foreach($cart_items as $data)
                        <form action="{{ route('change_item_amount',[$cart_info->id,$data->book_id]) }}" method="POST">
                        <tr>
                            <th>{{ show_books_name($data->book_id) }}</th>
                            <th>{{ show_books_author($data->book_id)  }}</th>
                            <th><input type="number" name="amount" id="amount" value="{{ $data->item_amount }}"></th>
                            <th>{{ $data->price }} tk</th>
                            <th><button type="submit" class="btn btn-success">Change</button></th>
                            <th><a class="btn btn-success" href="{{ route('delete_book',[$cart_info->id,$data->book_id] ) }}">Delete</a></th>
                            </form>
                                @endforeach
                                </table>
                            </div>
                            <h2 style="padding-left:865px">Total = {{ $cart_info->total_price }} tk</h2>
                        </div>
                    </form>
                </div>
            </div>
  </div>
@endsection

【问题讨论】:

  • 你不能在&lt;tr&gt;之外有&lt;form&gt;标签,这是无效的标记。 &lt;form&gt;必须包裹&lt;table&gt;或在&lt;td&gt;中(不是&lt;tr&gt;)。
  • 您已经关闭了两次表单标签。

标签: php laravel forms foreach


【解决方案1】:

最简单的解决方案可以是:将表单标签放入td,(顺便说一句,它应该是td,而不是th,你在thead中使用th):

<table style="border-collapse: collapse;width: 100%;">
    <tr style="border: 1px solid #dddddd;text-align: left;padding: 8px;font-size:20px;color: gray">
        <th>Book Name</th>
        <th>Author Name</th>
        <th>Book Amount</th>
        <th>Price</th>
        <th>Action</th>
        <th>Action</th>
    </tr>
@foreach($cart_items as $data)
    <tr>
        <td>{{ show_books_name($data->book_id) }}</td>
        <td>{{ show_books_author($data->book_id)  }}</td>
        <td>&nbsp; <!-- Here we have empty td now --> </td>
        <td>{{ $data->price }} tk</td>
        <td>
            <!-- Form is here now -->
            <form action="{{ route('change_item_amount',[$cart_info->id,$data->book_id]) }}" method="POST">
                <input type="number" name="amount" id="amount" value="{{ $data->item_amount }}">
                <button type="submit" class="btn btn-success">Change</button>
            </form>
        </td>
        <td><a class="btn btn-success" href="{{ route('delete_book',[$cart_info->id,$data->book_id] ) }}">Delete</a></td>
@endforeach
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-31
    • 2021-09-07
    • 2019-02-28
    • 2019-09-01
    • 2018-09-15
    • 2021-07-11
    • 2017-07-09
    • 2017-06-20
    相关资源
    最近更新 更多