Laravel 5.8 jQuery Ajax Form Submit

In this tutorial you will learn about the Laravel 5.8 jQuery Ajax Form Submit and its application with practical example.

Laravel 5.8 jQuery Ajax Form Submit

In this Laravel jQuery Ajax Form Submit tutorial you’ll learn to submit form data without reloading or refreshing of page using ajax. In this tutorial I’ll show you step by step how to submit a form without reloading or refreshing of page using ajax. In this example we will be using jquery ajax submit handler for ajax form submission.

In Laravel, to submit form data using ajax we must have to incorporate csrf token with form data and set X-CSRF-TOKEN request header with ajax form submit request.

Before starting with example I assume that you already have fresh laravel 5.8 installation ready, if you have it installed then you can skip this step.

Create Laravel 5.8 Application

First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command

Configure Database In .env file

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

.env

Generate Application Key

Open terminal and switch to the project directory and run the following command to generate application key and configure cache.

Set Default String Length

Locate the file “app/Providers/AppServiceProvider”, and add following line of code to the top of the file

and inside the boot method set a default string length as given below –

So this is how “app/Providers/AppServiceProvider” file looks like –

Create Model and Migration

Now, we have to define table schema for contact table. Open terminal and let’s run the following command to generate a Contact model along with a migration file to create contact table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. Lets open migration file created and put following code in it –

Run Laravel Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the contact table will be created in database.

Create Laravel View Files

In this step, we will create laravel view/blade file to display contact form and submit it using jquery ajax. Lets create a blade file “contact_form.blade.php” in “resources/views/ajaxFormSubmit/” directory.

Recommended:-  Laravel Clear View Cache

Create bootstrap form In View File

Go to “contact_form.blade.php” in “resources/views/ajaxFormSubmit/” directory and put the following code in it to create a bootstrap form.

resources/views/ajaxFormSubmit/contact_form.blade.php

Add CSRF Meta Tag In View file

Laravel comes with a security mechanism called csrf token. In Laravel, each of the request is attached a csrf_token that is validated before can be processed. In order to submit form data using ajax, we must have to incorporate following meta tag in our view file.

Add jQuery In View file

Now, we need to include jquery library into our view file. Add the following line of code inside the head section of view file.

Create a submit handler function

Lets create an empty function to be executed when submit button is clicked. Add the following javascript code in view file at the end of the view file.

Recommended:-  Laravel 5.8 Multiple Image Upload with jQuery Add More Button

Set CSRF Token In jQuery Ajax Header :-

In order to submit form data using ajax, we must have to set X-CSRF-TOKEN request header with ajax form submit request.

Create $.ajax() function to submit form data :-

Now, we will add following code to submit form data using ajax inside submit handler function. Our final submit handler function will look like this –

Now our final view file will look like this –

resources/views/ajaxFormSubmit/contact_form.blade.php

Create Laravel Controller

Next, we have to create a controller to display contact form and to handle form validation and submit operations. Lets Create a controller named ContactController using command given below –

Recommended:-  Laravel 5.8 Autocomplete Search Using Typeahead JS

Once the above command executed, it will create a controller file ContactController.php in app/Http/Controllers/ajaxFormSubmit directory.

Open the ajaxFormSubmit/ContactController.php file and put the following code in it.

app/Http/Controllers/ajaxFormSubmit/ContactController.php

In this controller, we have following methods –

index() :- To display contact form.

store() :- To handle form submit request.

Define Laravel Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Start Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/jquery-ajax-form-submit

Output 1:-

laravel-5-8-jquery-ajax-form-submit-1-1

Output 2:-

Form data submitted successfully

laravel-5-8-jquery-ajax-form-submit-2-2

In this tutorial we have learn about the Laravel 5.8 jQuery Ajax Form Submit and its application with practical example. I hope you will like this tutorial.