Im a beginner in laravel. I got problem try posting to route. i don’t use any form (form action="" Method="POST").
This is my button:
<div class="button0">
<button
type="button"
class="btn btn-primary"
style="margin:20px 150px 0 0; float: right; width:100px"
id="btnInsert"
>
Insert
</button>
</div>
Here’s my script:
$("#btnInsert").click(function () {
var productid = $("#productid").val();
var productname = $("#productname").val();
var productprice = $("#productprice").val();
var productdes = $("#productdes").val();
var productimage = $("#productimage").val();
var productcate = $("#cate").val();
if (productid == "") {
alert("Empty Product ID");
} else {
// it doesn't work after this
$.post("/addproduct", {
productid: productid,
productname: productname,
productprice: productprice,
productdes: productdes,
productimage: productimage,
productcate: productcate,
}),
function (result) {
if (result == 1) {
alert("Record has been added");
window.location.href = "/product";
}
};
}
});
this is my route:
Route::post('/addproduct',[ProductController::class, 'AddProduct']);
2
Answers
First of all hit these artisan commands:
or you can use
and after that correct your url: coz apis in api.php file
like
Hope , It will helps you
Your method is also good , but as you say you are beginner so i recommend you must read laravel documentation and practicing the MVC methodology.
Always Use Naming Routes, and make sure every route has different or unique name or you may create group of route
Naming Route
Group Route
Create Controller:
Create model
Product
and migration to connect or save data in table.I hope this will help you or you may ask anything you want.
Happy Coding!