Posting with ajax and jquery has been covered so many times on this forum probably hundreds. I've given numerous examples myself.
Google:
site:laracasts.com ajax post
or
site:laracasts.com jquery ajax post
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a standard invoice with header info and multiple products in an html table. Can save header information but cannot figure out how to save the products. I am using a Modal form with Ajax to send data to Controller. My Ajax code is as under :
$('#saveBtn').click(function (e) {
e.preventDefault();
$(this).html('Sending..');
var tbldata = tbltoarr();
$.ajax({
data: $('#purchaseForm').serialize(),
url: "{{ route('purchases.store') }}",
type: "POST",
dataType: 'json',
success: function (data) {
$('#purchaseForm').trigger("reset");
$('#code').focus();
},
error: function (data) {
var data2 = JSON.parse(data.responseText);
$('.alert-danger').html('');
jQuery.each(data2.errors, function(key, value){
jQuery('.alert-danger').show();
jQuery('.alert-danger').append('<li>'+value+'</li>');
});
}
});
});
and Controller code is :
public function store(Request $request)
{
$result = Purchase::updateOrCreate(['id' => $request->id],
[ 'date' => $request->date,
'suppcode' => $request->code,
'suppinv' => $request->invno,
'suppdate' => $request->invdate,
]);
return response()->json(['success'=>'Account saved successfully.']);
}
tbldata is an array of items added to invoice, with following columns : Itemcode,Itemdesc,qty,rate,amount My question is how to attach tbldata to ajax post request and then how to handle/save it in controller.
Please or to participate in this conversation.