【发布时间】:2019-02-25 21:20:54
【问题描述】:
我以前从未使用过 ajax,但在我当前的项目中,我看到了这样做的必要性。我有一张发票表,其中有一个名为“is_confirmed”的字段,默认情况下设置为 false。
在 index.blade.php 中,我显示了当前登录用户发出的所有发票。在表的每一行中,一旦用户单击确认按钮,该行就会更新,并且数据库中的“is_confirmed”字段设置为“true”。现在的问题是他们的确认按钮仍然处于活动状态,这意味着用户仍然可以单击它。
您将如何实现这一点,以便“is_confirmed”字段设置为“true”的所有行都将禁用其按钮,而“is_confirmed”字段设置为“false”的行是唯一具有可点击按钮的行,即使在页面刷新时也是如此.
这是我的 index.blade.php,它当前显示所有已发送的发票。与确认按钮一起更新每行中的“is_confirmed”数据库字段:
@section('content')
@foreach($sentinvoices as $sentinvoice)
<tr>
<td>
<span>{{ $sentinvoice->recipient->fullname }}</span>
</td>
<td>
<span>{{$sentinvoice->updated_at->format("M d, Y")}}</span>
</td>
<td>
<span>{{$sentinvoice->status}}</span>
</td>
<td>
<span>{{$sentinvoice->amount}}</span>
</td>
<td class="text-center">
<form method="POST" action="{{ route('sender.confirm', $sentinvoice->uuid) }}" id="ajax">
@csrf
<input type="hidden" name="sender_id" value="{{$sentinvoice->sender_id}}">
<input type="hidden" name="is_confirmed" value="{{$sentinvoice->is_confirmed}}">
<button class="btn btn-success btn-sm" type="submit" id="confirm">Confirm</button>
</form>
</td>
</tr>
@endforeach
@endsection
我还在index.blade.php的底部添加了ajax函数如下:
@section('scripts')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$("#ajax").click(function(event) {
event.preventDefault();
$.ajax({
type: "post",
url: "{{ url('sender.confirm') }}",
dataType: "json",
data: $('#ajax').serialize(),
success: function(data){
alert("Data Save: " + data);
$("#confirm").prop('disabled', true);
},
error: function(data){
alert("Error")
}
});
});
</script>
@endsection
这是我在 InvoiceController 中提交表单的函数:
public function confirmInvoice(Request $request, $uuid)
{
$user = auth()->user();
$sentinvoices = Invoice::where('uuid', $uuid)->first();
$sentinvoices->sender_id = $user->id;
$sentinvoices->is_confirmed = 1;
$sentinvoices->save();
return redirect()->back();
}
我已经检查了其他类似问题的答案,但仍然无法正常工作。请在这里帮助我。
【问题讨论】:
-
呈现
invoices的代码在哪里? -
我已通过添加呈现发票的代码来更新帖子。非常感谢您的帮助,谢谢。