Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel dynamically add remove table row

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 5.7 - Dynamically Add or Remove input fields using JQuery</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  

 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>


<div class="container">
    <h2 align="center">Laravel 5.7 - Dynamically Add or Remove input fields using JQuery - Pakainfo.com</h2>  
    <div class="form-group">
         <form name="product_name" id="product_name">  
            <div class="alert alert-danger error-message-display" style="display:none">
            <ul></ul>
            </div>
            <div class="alert alert-success print-success-msg" style="display:none">
            <ul></ul>
            </div>
            <div class="table-responsive">  
                <table class="table table-bordered" id="dynamic_field">  
                    <tr>  
                        <td><input type="text" name="name[]" placeholder="Enter your 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('addProduct'); ?>";
      var i=1;  

      $('#add').click(function(){  
           i++;  
           $('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="name[]" placeholder="Enter your Product 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>');  
      });  

      $(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:postURL,  
                method:"POST",  
                data:$('#product_name').serialize(),
                type:'json',
                success:function(data)  
                {
                    if(data.error){
                        previewMessage(data.error);
                    }else{
                        i=1;
                        $('.dynamic-added').remove();
                        $('#product_name')[0].reset();
                        $(".print-success-msg").find("ul").html('');
                        $(".print-success-msg").css('display','block');
                        $(".error-message-display").css('display','none');
                        $(".print-success-msg").find("ul").append('<li>Record Inserted Successfully.</li>');
                    }
                }  
           });  
      });  


      function previewMessage (msg) {
         $(".error-message-display").find("ul").html('');
         $(".error-message-display").css('display','block');
         $(".print-success-msg").css('display','none');
         $.each( msg, function( key, value ) {
            $(".error-message-display").find("ul").append('<li>'+value+'</li>');
         });
      }
    });  
</script>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Php :: facetwp listing template archive 
Php :: movies -inurl:(htm|html|php|pls|txt) intitle:index.of “last modified” (mp4|wma|aac|avi) 
Php :: @forelse laravel 
Php :: replace special characters from string in codeigniter 
Php :: laravel How do I chain multiple where and orWhere clause in laravel from an Array [duplicate] 
Php :: laravel This package is not auto-updated. Please set up the GitHub Hook for Packagist so that it gets updated whenever you push! 
Php :: compare in wp 
Php :: wordpress html classes 
Php :: join with 2 table where id match in table 1 comma separated 
Php :: wordpress set category front end 
Php :: upload laravel 
Php :: Befreie den WordPress-Header von unnötigen Einträgen 
Php :: ob_start store variable in php 
Php :: extract email from text 
Php :: featured image tab not displayed on post 
Php :: How to automail in php 
Php :: php domdocument get elements one by one 
Php :: import facade laravel 
Php :: set php version for a domain with whm api 
Php :: js data php 
Php :: laravel eager loading pass variable in withCount where condition 
Php :: singular from table laravel 
Php :: the_field 
Php :: php execute script wait for response 
Php :: php upload image to another subdomain 
Php :: WP DELETE UNUSED TAGS 
Php :: customize response body with filters laravel 
Php :: HP officejet pro 8720 default password 
Php :: woocommerce php customer reset password length 
Php :: syntax error, unexpected variable "$result" in D:wordpressxampphtdocs empleteuser_delete.php on line 13 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =