Get parameter from ajax to controller Laravel

11,941

Solution 1

You can not send GET value in data. Send it in the URL as a query string. Check this code:

$(document).ready(function () {


        $(document).on('click', '.brnch_clk', function (e) {
            e.preventDefault();

            //alert('ok')
            var branchid = $(this).data('branch');
            $('#txt_branchid').val(branchid);
            //alert($(this).data('branch'));

            $.ajax({
                'url': 'search?id='+branchid,
                'type': 'GET',
                'data': {},

                success: function(response){ // What to do if we succeed
                    if(data == "success")
                        alert(response);
                },
                error: function(response){
                    alert('Error'+response);
                }



            });
        });

    });

Solution 2

I suppose 'url': 'search?id='+branchid in place of 'url': 'search' should force JQuery to send data as GET parameters. Otherwise, data is sent as POST parameters.

Share:
11,941
The Rock
Author by

The Rock

Updated on June 28, 2022

Comments

  • The Rock
    The Rock almost 2 years

    I want to pass value of id to controller in Laravel

    my Ajax Code:

    $(document).ready(function () {
    
    
                $(document).on('click', '.brnch_clk', function () {
                    //alert('ok')
                    var branchid = $(this).data('branch');
                    $('#txt_branchid').val(branchid);
                    alert($(this).data('branch'));
    
                    $.ajax({
                        'url': 'search',
                        'type': 'GET',
                        'data': {id: branchid},
    
                        success: function(response){ // What to do if we succeed
                            if(data == "success")
                                alert(response);
                        },
                        error: function(response){
                            alert('Error'+response);
                        }
    
    
    
                    });
                });
    
            });
    

    My Controller:

    public function search(Request $request)
    {
        $member = $request->get('id');
        return json_encode($member);
    }
    

    My Route:

    Route::get('search', 'ShowstaffController@search');
    

    My Link:

    <a href="{{URL('search')}}" class="brnch_clk"
        data-branch="{{$value->branch_id}}"> {{$value->Branch_Name}}
    </a>
    

    How can i get parametter id from ajax to controller??

    • Mihai Alexandru-Ionut
      Mihai Alexandru-Ionut over 6 years
      I suppose $member = $request->get('id'); does not work.
    • The Rock
      The Rock over 6 years
      @Alexandru-IonutMihai yes i get blank result how can i get a real result?
    • Mihai Alexandru-Ionut
      Mihai Alexandru-Ionut over 6 years
      try this : $request->query('id');
    • The Rock
      The Rock over 6 years
      @Alexandru-IonutMihai result still blank
    • Mihai Alexandru-Ionut
      Mihai Alexandru-Ionut over 6 years
      Try using : $id = $_GET['id'];
    • The Rock
      The Rock over 6 years
      @Alexandru-IonutMihai : Error Undefined index: id
  • The Rock
    The Rock over 6 years
    still can't get the result because still out put blank
  • The Rock
    The Rock over 6 years
    still get blank result
  • The Rock
    The Rock over 6 years
    still get blank result
  • The Rock
    The Rock over 6 years
    Result still blank
  • The Rock
    The Rock over 6 years
    When i change url like yours should i update my route and link with parameter id??
  • Hasan Shahriar
    Hasan Shahriar over 6 years
    nop, open Network tab on your browser, and check request data. Can you see the id when you click .brnch_clk ?
  • kharhys
    kharhys over 6 years
    Pass in parameter via route. Route::get('search/{id}', 'ShowstaffController@search'); and public function search($id)