Richie Zakaria
Richie Zakaria

Reputation: 91

Laravel request always return null

So i have this view

<form method="POST" action="/store">
<div class="control-group after-add-more">  
<div class="form-group">                                                                    
<label>Partner :</label>                                                                        
<input type="text" class="form-control" id="partner" name="partner" value="" placeholder="Partner Name" />                                                                  
</div>                                                          
<button class="btn btn-success add-more" type="button">                                                         
<i class="glyphicon glyphicon-plus"></i> Plus Partner</button>                                                      
                                            
<div class="copy hide">                                                         
<div class="control-group">                                                                     
<div class="form-group">                                                                        
<label> Partner :</label>                                                                       
<input type="text" class="form-control" id="partner2" name="partner2" value="" placeholder="Partner Name" />                                                                    
</div>                                                                                                                              
<button class="btn btn-danger remove" type="button">                                                                    
<i class="glyphicon glyphicon-remove"></i>                                                          
Delete Partner</button>
</div>      
                                            
</div>
<button type="submit">Submit</button>
</form>

this is jquery that i use

$(document).ready(function() {
         $(".add-more").click(function() {
            var html = $(".copy").html();
            $(".after-add-more").after(html);
        });
        $("body").on("click", ".remove",function() {
            $(this).parents(".control-group").remove();
        });
});

but when i try to submit , the input partner2 is returning null value

this is my controller

$kerma = Kerma::find(1)->get();
$kerma->partner()->createMany(['partner' => $request->input('partner')],['partner' => $request->input('partner2')])

anyone have a solution for me ? thanks in advance

Upvotes: 1

Views: 625

Answers (1)

Harshana
Harshana

Reputation: 5476

When submitting your request to the form:

  1. The routes file should have the POST method configured.
Route::post('store', 'YourController@yourmethod');
  1. Addition of CSRF token to the Form to prevent Page Expired issue. So, your blade file should be like below:
<form method="POST" action="{{url('store')}}">
@csrf
<div class="control-group after-add-more">  
<div class="form-group">                                                                    
<label>Partner :</label>                                                                        
<input type="text" class="form-control" id="partner" name="partner" value="" placeholder="Partner Name" />                                                                  
</div>                                                          
<button class="btn btn-success add-more" type="button">                                                         
<i class="glyphicon glyphicon-plus"></i> Plus Partner</button>                                                      
                                            
<div class="copy hide">                                                         
<div class="control-group">                                                                     
<div class="form-group">                                                                        
<label> Partner :</label>                                                                       
<input type="text" class="form-control" id="partner2" name="partner2" value="" placeholder="Partner Name" />                                                                    
</div>                                                                                                                              
<button class="btn btn-danger remove" type="button">                                                                    
<i class="glyphicon glyphicon-remove"></i>                                                          
Delete Partner</button>
</div>      
                                            
</div>
<button type="submit">Submit</button>
</form>

Upvotes: 1

Related Questions