vendredi 29 novembre 2019

Submitting a form with ajax and laravel

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 TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'. on console. How can I submit the form without refreshing the page?

Blade

 <form method="POST" role="form" enctype="multipart/form-data">
        

           <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=""  ></option>
             @endforeach
           </select>

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

           <button type="button" onclick = "submitThisForm()" class="btn btn-primary">Submit</button>

    </form> 

Ajax

<script>
function submitThisForm(id){
    let url = "".replace(':id', id);
    $.ajax( {
        url: url,
        type: 'POST',
        data: new FormData( this ),
        processData: false,
        contentType: false,
        success: function(result){
            console.log(result);
        }
    } );
    e.preventDefault();


}
</script>

Route

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


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire