I have never used ajax before, but in my current project, I'm seeing the need to. I have a table of invoices which has a field called "is_confirmed" which is set to false by default.
In the index.blade.php, I have displayed all invoices that was sent out by the currently logged in user. In each row of the table, once the user clicks the confirm button,that row is updated and the "is_confirmed" field is set to "true" in the database. The problem now is that they still have their confirm button active which means the user can still click it.
How would you implement this such that all rows whose "is_confirmed" field set to "true" will have their buttons disabled and those whose "is_confirmed" field set to "false" are the only ones with clickable button even upon page refresh.
Here's my index.blade.php that currently displays all sent invoices. Along with the confirm button to update the "is_confirmed" database field in each row:
@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
I also added ajax function at the bottom of the index.blade.php as follows:
@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
Here's my function in the InvoiceController that does the form submission:
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();
}
I have checked answers to other similar questions, still I couldn't get this to work. Please help me here.

invoices?