skip to Main Content

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


  1. First of all hit these artisan commands:

    php artisan route:clear
    php artisan route:cache
    

    or you can use

    php artisan optimize:clear
    php artisan optimize
    

    and after that correct your url: coz apis in api.php file
    like

    $.post("api/addproduct", {
    

    Hope , It will helps you

    Login or Signup to reply.
  2. 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

    Add Controller in web.php | api.php on top.
    
    
    use AppHttpControllersProductController;
    
    Route::post('/addproduct', [ProductController::class, 'AddProduct'])->name('add.product');
    

    Group Route

     Route::controller(ProductController::class)->group(function () {
        Route::post('/addproduct','AddProduct')->name('add.product');
        Route::get('/listproduct','ListProduct')->name('list.product');
    });
    
    <form action="{{ route('add.product') }}" method="post" enctype="multipart/form-data"
                                    autocomplete="off">
                                    @csrf
                                    <div class="row">
                                        
                                        <div class="col-sm-12">
                                            <div class="row" style="margin-top: 15px;">
                                                <div class="col-sm-12">
                                                    <div class="row">
                                                        <div class="col-sm-6">
                                                            <label for="horizontal-main-course-input"
                                                                class="col-sm-5 col-form-label">Name</label>
                                                            <div class="col-sm-12">
                                                                <input type="text" class="form-control" name="name"
                                                                    id="name" placeholder="Enter Name Here">
    
                                                                @if ($errors->has('name'))
                                                                    <span
                                                                        class="text-danger">{{ $errors->first('name') }}</span>
                                                                @endif
                                                            </div>
                                                        </div>
                                                        <div class="col-sm-6">
                                                            <label for="horizontal-main-course-input"
                                                                class="col-sm-5 col-form-label">Email</label>
                                                            <div class="col-sm-12">
                                                                <input type="text" class="form-control" name="price"
                                                                    id="price"
                                                                    placeholder="Enter Price Here">
    
                                                                @if ($errors->has('price'))
                                                                    <span
                                                                        class="text-danger">{{ $errors->first('price') }}</span>
                                                                @endif
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <br />
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <div class="col-sm-12" style="text-align: center;">
                                                <button type="submit" class="btn btn-primary w-md">SAVE</button>
                                            </div>
                                        </div>
                                    </div>
                                </form>

    Create Controller:

    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    use AppModelsProduct;
    
    class ProductController extends Controller
    {
        public function index()
        {
            // Logic to display a list of products
        }
    
        public function create()
        {
            // Return a view for creating a new product
            return view('products.create');
        }
    
        public function store(Request $request)
        {
            // Validate and store the product
            $product = new Product();
            // Assign values from $request to $product properties
            $product->productid = $request->input('productid');
            $product->productname = $request->input('productname');
            // ... other properties
            $product->save();
    
            // Redirect to the products index page or do something else
            return redirect()->route('products.index');
        }
    }
    
    

    Create model Productand migration to connect or save data in table.
    I hope this will help you or you may ask anything you want.
    Happy Coding!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search