In this tutorial you will learn about the Laravel Dynamically Add or Remove Input Fields jQuery and its application with practical example.
In this Laravel Dynamically Add or Remove Input Fields jQuery tutorial I’ll show you how to dynamically add or remove input field in form using jQuery in laravel. In this tutorial you will learn to dynamically add or remove input field in form using jquery in laravel. In this tutorial I’ll share example to demonstrate Dynamically Add or Remove Input Fields jQuery.
Laravel Dynamically Add or Remove Input Fields jQuery
- Step 1: Install Laravel App
- Step 2: Add Database Details
- Step 3: Create Migration & Model
- Step 4: Add Routes
- Step 5: Create Controller by Artisan
- Step 6: Create Blade View
- Step 7: Run Development Server
Step 1: Install Laravel App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Step 2: Add Database Details
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.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3: Create Migration & Model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Todo -m |
The above command will create one model name Todo.php and as well as one migration file for the Todos table.
Now go to database/migrations folder and open create_todos_table.php. Then update the following code into create_todos_table.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateTodosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('todos', function (Blueprint $table) { $table->id(); $table->string('title'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('todos'); } } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 4: Create 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
1 2 |
Route::get('add-remove-input-fields', 'AddRemoveFieldController@index'); Route::post('add-remove-input-fields', 'AddRemoveFieldController@store'); |
Step 5: Generate Controller by Artisan
Now, lets create a controller named AddRemoveFieldController using command given below –
1 |
php artisan make:controller AddRemoveFieldController |
This command will create a controller named AddRemoveFieldController.php file.
Next, Navigate to app/http/controllers/ folder and open AddRemoveFieldController.php. Then add the following file uploading methods into your AddRemoveFieldController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Todo; use Illuminate\Support\Facades\Validator; class AddRemoveFieldController extends Controller { public function index() { return view("add-remove-input-fields"); } public function store(Request $request) { $data = []; foreach($request->input('title') as $key => $value) { $data["title.{$key}"] = 'required'; } $validator = Validator::make($request->all(), $data); if ($validator->passes()) { foreach($request->input('title') as $key => $value) { Todo::create(['title'=>$value]); } return response()->json(['success'=>'true']); } return response()->json(['error'=>$validator->errors()->all()]); } } |
Step 6: Create Blade View
In this step, we will create one blade view file named add-remove-input-fields.blade.php.
Now, go to resources/views and create one file name add-remove-input-fields.blade.php Then update the following code into your add-remove-input-fields.blade.php file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
<!DOCTYPE html> <html> <head> <title>Laravel Dynamically Add or Remove input fields using JQuery</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/js/bootstrap.min.js"></script> <meta name="csrf-token" content="{{ csrf_token() }}"> <style type="text/css"> body { background-color: #edf2f7; } </style> </head> <body> <div class="container mt-3"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header"> <h2 class="text-success">Laravel Dynamically Add or Remove input fields using JQuery Demo</h2> </div> <div class="card-body"> <div class="form-group"> <form name="add_name" id="add_name"> <div class="alert alert-danger show-error-message" style="display:none"> <ul></ul> </div> <div class="alert alert-success show-success-message" style="display:none"> <ul></ul> </div> <div class="table-responsive"> <table class="table table-bordered" id="dynamic_field"> <tr> <td><input type="text" name="title[]" placeholder="Enter title" class="form-control name_list" / id="title"></td> <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td> </tr> </table> <input type="button" name="submit" id="submit" class="btn btn-primary" value="Submit" /> </div> </form> </div> </div> </div> </div> </div> </div> <script type="text/javascript"> $(document).ready(function(){ var url = "{{ url('add-remove-input-fields') }}"; var i=1; $('#add').click(function(){ var title = $("#title").val(); i++; $('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="title[]" placeholder="Enter title" class="form-control name_list" value="'+title+'" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>'); }); $(document).on('click', '.btn_remove', function(){ var button_id = $(this).attr("id"); $('#row'+button_id+'').remove(); }); $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $('#submit').click(function(){ $.ajax({ url:"{{ url('add-remove-input-fields') }}", method:"POST", data:$('#add_name').serialize(), type:'json', success:function(data) { if(data.error){ display_error_messages(data.error); }else{ i=1; $('.dynamic-added').remove(); $('#add_name')[0].reset(); $(".show-success-message").find("ul").html(''); $(".show-success-message").css('display','block'); $(".show-error-message").css('display','none'); $(".show-success-message").find("ul").append('<li>Todos Has Been Successfully Inserted.</li>'); } } }); }); function display_error_messages(msg) { $(".show-error-message").find("ul").html(''); $(".show-error-message").css('display','block'); $(".show-success-message").css('display','none'); $.each( msg, function( key, value ) { $(".show-error-message").find("ul").append('<li>'+value+'</li>'); }); } }); </script> </body> </html> |
Step 7: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/add-remove-input-fields |