skip to Main Content

I’m trying to add product to the database with Ajax without refreshing the page and send the data to the database but I get an error Uncaught ReferenceError: $ is not defined on console. How can I submit the form without refreshing the page?

Blade

 <form action="#" id="add-productForm" method="POST" role="form" enctype="multipart/form-data">
            {{csrf_field()}}

               <label for="pro_name">Name</label>
               <input type="text" class="form-control" name="pro_name" id="pro_name" placeholder="Enter product name">

               <label  for="category_id">Choose Category</label>
               <select name="category_name" id="category_name">
               <option value=""> --Select Category -- </option>
               @foreach ($categoryname_array as
                 $data)
                 <option value="{{ $data->name }}"  >{{$data->name}}</option>
                 @endforeach
               </select>

               <label for="photos">Choose 5 Images</label>
               <input  "multiple="multiple" name="photos[]" type="file">

            <button type="button" class="btn btn-primary">Submit</button>
        </form> 

Route

Route::post('seller/product', 'ProductController@store')->name('product.store');

Ajax

 <script>
$(document).on("click", "#save", function(e) {
    let url = "{{route('product.store')}}";
    e.preventDefault();
    $.ajax({
        type: "post",
        url: url,
        data: $(".add-productForm").serialize(),

        success: function(store) {

        },
        error: function() {
        }
    });
});
</script>

2

Answers


  1. Common problem, before script you need to load your jQuery library.

    <script src="/your_path/jquery.plugin.js"></script>
    <script src="/your_path/jquery.min.js"></script>
    

    More: https://javarevisited.blogspot.com/2016/04/3-ways-to-solve-jquery-uncaught-reference-error-is-not-defined.html

    Login or Signup to reply.
  2. For “Uncaught ReferenceError: $ is not defined” this error you need to add the jquery supporting file in html page:

    '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> 
    </script>'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search