php - POST 方法
问题描述
在@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
解决方案
最简单的解决方案可以是:将表单标签放入td
, (顺便说一句,它应该是td
,而不是th
,你使用th
in thead
):
<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> <!-- 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>
推荐阅读
- javascript - 使用 Firestore REST API 在结构化查询中设置“orderBy”
- java - Spring 将 fixedRateString 安排为 Duration
- php - PHP mysqli 子查询返回 Impossible WHERE 在读取 const 表后注意到
- html - 如何删除导航下方不需要的空格?
- css - 即使删除规则,如何完成动画?
- python - Seaborn barplot 图例标签失去颜色
- swift - 访问已使用 Padding 扩展的 CVPixelBuffer 之外的像素
- html - 获取三角形以显示在 div 之外,漂浮在它旁边的 div 上
- javascript - 当 console.logging 组件内的窗口对象时,它会给出“未定义”。但是在使用浏览器控制台时可以访问
- javascript - Tablesorter stickyHeaders 小部件不起作用