Laravel Add Remove input fields Dynamically using jQuery

Laravel Add Remove input fields Dynamically using jQuery

In this Post We Will Explain About is Laravel Add Remove input fields Dynamically using jQuery With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to dynamically add input fields and submit to database with laravel Example

In this post we will show you Best way to implement laravel dynamic form fields validation example, hear for Laravel – Dynamically Add or Remove input fields using JQuery with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

laravel add more fields example

Step 1 : Install Laravel Application

composer create-project --prefer-dist laravel/laravel blog

Step 2: Database Configuration

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=live here personal your database name(infinityknow)
DB_USERNAME=live here personal database username(root)
DB_PASSWORD=live here personal database password([email protected]#$%)

Step 3: Create studentslist Table and Model

php artisan make:migration create_studentslist_table

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateStudentslistTable extends Migration
{
<!-- Devloped by infinityknow.com free download examples -->
    public function up()
    {
        Schema::create('studentslist', function (Blueprint $table) {
           $table->increments('id');
            $table->string('name');
            $table->timestamps();

        });
    }
<!-- Devloped by infinityknow.com free download examples -->
    public function down()
    {
        Schema::dropIfExists('studentslist');
    }
}

php artisan migrate

php artisan make:model StudentsList

app/StudentsList.php

Ok, so after some most imp run bellow command we will find like as a app/StudentsList.php and put bellow some source code content in StudentsList.php file:

READ :  JQuery Ajax Add Remove Input Fields Dynamically

<?php
<!-- Devloped by infinityknow.com free download examples -->
namespace App;
use Illuminate\Database\Eloquent\Model;
class StudentsList extends Model
{
    public $table = "studentslist";
    public $fillable = ['name'];
}

Step 4: Add Routes(routes/web.php)

Route::get("addStud","[email protected]");

Route::post("addStud","[email protected]");

Step 5: Create StudController

php artisan make:controller StudController

And then bellow some useful command We will find new simple file in this path Like as a app/Http/Controllers/StudController.php.
In this laravel controller will make seven methods by selected default as bellow some function or methods:
1)addStud()
2)addStudStudents()
So, let’s copy some source code bellow code and put on StudController.php file.

app/Http/Controllers/StudController.php

<?php
<!-- Devloped by infinityknow.com free download examples -->
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\StudentsList;
use Validator;
class StudController extends Controller
{
    public function addStud()
    {
        return view("addStud");
    }
	
	<!-- Devloped by infinityknow.com free download examples -->
    public function addStudStudents(Request $request)
    {
        $rules = [];
        foreach($request->input('name') as $key => $value) {

            $rules["name.{$key}"] = 'required';

        }
		<!-- Devloped by infinityknow.com free download examples -->
        $validator = Validator::make($request->all(), $rules);
        if ($validator->passes()) {
            foreach($request->input('name') as $key => $value) {
                StudentsList::create(['name'=>$value]);
            }
            return response()->json(['success'=>'done']);
        }
        return response()->json(['error'=>$validator->errors()->all()]);
    }
}

Step 6: Create Blade File

And then just make following the simple file and put bellow source code.

READ :  Dropbox API - Laravel Oauth Dropbox File Upload Steps

resources/views/addStud.blade.php

<!DOCTYPE html>

<html>
<head>
    <title>Laravel - step by step  Dynamically Add or Remove some dynamic-added input fields using JQuery</title>
    <link rel="stylesheet" href="bootstrap.min.css" />  
    <script src="1.12.4/jquery.min.js"></script>
    <script src="bootstrap.min.js"></script>  
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>

<body>
<!-- Devloped by infinityknow.com free download examples -->
<div class="container">
    <h2 align="center">Laravel - Steps Dynamically Add or Remove Data  from input fields using JQuery</h2>  
    <div class="form-group">
	<!-- Devloped by infinityknow.com free download examples -->
         <form name="add_stud_name" id="add_stud_name">  
            <div class="alert alert-danger live-error-message" style="display:none">
            <ul></ul>
            </div>
            <div class="alert alert-success live-success-message" style="display:none">
            <ul></ul>
            </div>
            <div class="table-responsive">  
                <table class="table table-bordered" id="get_dynemic_field">  
                    <tr>  
                        <td><input type="text" name="name[]" placeholder="Enter your First Name" class="form-control name_list" /></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-info" value="Submit" />  
            </div>
         </form>  
    </div> 
</div>
<script type="text/javascript">
    $(document).ready(function(){      
      var postURL = "<?php echo url('addStud'); ?>";
      var i=1;  

      $('#add').click(function(){  
           i++;  
           $('#get_dynemic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="name[]" placeholder="Enter your Fname Name" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');  

      });  
<!-- Devloped by infinityknow.com free download examples -->
      $(document).on('click', '.btn_remove', function(){  
           var livebtn_id = $(this).attr("id");   
           $('#row'+livebtn_id+'').remove();  
      });  

      $.ajaxSetup({
          headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });
<!-- Devloped by infinityknow.com free download examples -->
      $('#submit').click(function(){            
           $.ajax({  
                url:postURL,  
                method:"POST",  
                data:$('#add_stud_name').serialize(),
                type:'json',
                success:function(data)  
                {
                    if(data.error){
                        printErrorMsg(data.error);
                    }else{
                        i=1;
                        $('.dynamic-added').remove();
                        $('#add_stud_name')[0].reset();
                        $(".live-success-message").find("ul").html('');
                        $(".live-success-message").css('display','block');
                        $(".live-error-message").css('display','none');
                        $(".live-success-message").find("ul").append('<li>some Record Inserted Successfully.</li>');
                    }
                }  
           });  
      });  
	  <!-- Devloped by infinityknow.com free download examples -->
      function printErrorMsg (msg) {
         $(".live-error-message").find("ul").html('');
         $(".live-error-message").css('display','block');
         $(".live-success-message").css('display','none');
         $.each( msg, function( key, value ) {
            $(".live-error-message").find("ul").append('<li>'+value+'</li>');
         });
      }
    });  
</script>
</body>
</html>

php artisan serve

http://localhost:8000/addStud

You are Most welcome in my youtube Channel Please shubscibe my channel. and give me FeedBackMore Details……
Angularjs Example

READ :  Angular Autocomplete Textbox PHP MySQLi

Example

I hope you have Got What is Laravel – Dynamically Add or Remove input fields using JQuery And how it works.I would Like to have FeedBack From My Blog(infinityknow.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(infinityknow.com) Are Most Always Welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *