fsales
1 month ago
104
2
Laravel

Laravel - Dynamically Add or Remove input fields using JQuery

Posted 1 month ago by fsales

good morning devs, I'm having a problem I'm trying to use my system to add entry and remove entry and I'm not getting

Contrato Ação
                            <td><button type="button" name="add" id="add" class="btn btn-success">+</button></td>  
   </tr>  

script

$(document).ready(function(){ var postURL = ""; var i=1;
  $('#add').click(function(){  
       i++;  
       $('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="contrato_1[]" placeholder="Enter your 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:$('#add_name').serialize(),
            type:'json',
            success:function(data)  
            {
                if(data.error){
                    printErrorMsg(data.error);
                }else{
                    i=1;
                    $('.dynamic-added').remove();
                    $('#add_name')[0].reset();
                    $(".print-success-msg").find("ul").html('');
                    $(".print-success-msg").css('display','block');
                    $(".print-error-msg").css('display','none');
                    $(".print-success-msg").find("ul").append('<li>Record Inserted Successfully.</li>');
                }
            }  
       });  
  });  

});  
------------------ controller ------------------

public function store(Request $request) {

    $request->validate([
        'cod_cliente' => 'required',
        'nome' => 'required',
        'email' => 'required|email|unique:users',
        'password' => 'required|min:8',
        'razao' => 'required',
        'documento' => 'required|min:11|max:18',
        'status' => 'required',
        'contrato' => 'required',
        'valor' => 'required'
    ]);
    
    $rules = [];


    foreach($request->contrato_1 as $key => $value) {
        $rules["contrato_1.{$key}"] = 'required';
    }

    foreach($request->contrato_1 as $key => $value) {
        Cliente::create(['contrato_1'=>$value]);
    }   

    $permissao = Role::where('name', 'cliente')->first();

      $user = User::create([
            'name' => $request->nome,
            'email' => $request->email,
            'password' => bcrypt($request->password)
        ]);

      $user->roles()->attach($permissao->id);

    Cliente::create([
        'user_id' => $user->id,
        'cod_cliente' =>$request->cod_cliente,
        'nome' => $request->razao,
        'documento' => $request->documento,
        'status' => $request->status,
        'contrato' => $request->contrato,
        'valor' => $request->valor
    ]);        
    
    
       
    return redirect()->route('admin.cadastro.cliente')->with('cadastrado', 'Registrado com sucesso!');
}

Please sign in or create an account to participate in this conversation.