bipin's avatar
Level 2

how to pass value from js to controller

how i can pass this this value to my controller any ideas?

            <div id="container">
            <h2>D</h2>                    
                                           <form method="post"  action="{{url('select2')}}">
                           {{ csrf_field() }}

                        <div id="form1">
                        </div>
                    <div >  <input type="submit" name="submit" type="button">
            </div>
            
        </form>

            </div>       
                    <script type="text/javascript">
                    $(document).ready(function() {
                    debugger

        var i=0;
        var input=[];
                    @foreach($result as $s)
                    
                    @if($s->fieldtype=='text')
                    
                     $("#form1").append('<div class="row"><label>{{$s->fieldname}}
   </label><input type="text" name="name" id="name.concat({{$s->id}})"></div>');
                    
                       
                     @elseif($s->fieldtype=='textarea')
                     $("#form1").append('<div class="row">{{$s->fieldname}}<input 
   type="text" id="{{$s->id}}"><input type="hidden" value="{{$s->id}}" id="name"> </div>');
                     
                     @endif
                     
                     input[i++] = document.getElementById("{{$s->id}}").value;

                      @endforeach

                  $("#submit").click(function() {
                     $.ajax({
                url:'/select2',
                type: 'POST',
                dataType:'json',
                contentType: 'application/json',

               data: {'data': input, '_token' : $('#_token').val()}
            });

        });   
    });
                    </script>
0 likes
8 replies
Snapey's avatar

You are not explaining what the problem is or what you are trying to do?

1 like
fahad's avatar

you are already sending the ajax request to the route "/select2", if you still haven't define your route and it's handler controller

in your route.php

Route::post('/select2', 'Select2Controller@methodName');

// replace the controller and methodName as your definition goes 

and later in your Seletc2Controller.php ( App/http/controllers/select2Controller.php)

public function methodName(Request $request){
   dd( $request->all());  // you should see all of your ajax passed values
}
1 like
vishal_mohan's avatar

in web.php file

    Route::post('salesMeetings/ajaxAdd', array('as' => 'select2', 'uses' => 
    'SalesMeetingController@ajaxAdd')); // for Ajax data add

Then------------------

  $("#submit").click(function() { 

       $.ajax({

            url: "{{ URL::route('select2') }}",

            type: 'POST',

            dataType:'json',

            contentType: 'application/json',

           data: {'data': input, '_token' : $('#_token').val()}

        }); 

  });
1 like

Please or to participate in this conversation.