12

How do I pass the id from this ajax call to the TestController getAjax() function? When I do the call the url is testUrl?id=1

Route::get('testUrl', 'TestController@getAjax');

<script>
    $(function(){
       $('#button').click(function() {
            $.ajax({
                url: 'testUrl',
                type: 'GET',
                data: { id: 1 },
                success: function(response)
                {
                    $('#something').html(response);
                }
            });
       });
    });    
</script>

TestController.php

public function getAjax()
{
    $id = $_POST['id'];
    $test = new TestModel();
    $result = $test->getData($id);

    foreach($result as $row)
    {
        $html =
              '<tr>
                 <td>' . $row->name . '</td>' .
                 '<td>' . $row->address . '</td>' .
                 '<td>' . $row->age . '</td>' .
              '</tr>';
    }
    return $html;
}

3 Answers 3

17

In the end, I just added the parameter to the Route::get() and in the ajax url call too. I changed $_POST['id'] to $_GET['id'] in the getAjax() function and this got my response back

Route::get('testUrl/{id}', 'TestController@getAjax');

<script>
    $(function(){
       $('#button').click(function() {
            $.ajax({
                url: 'testUrl/{id}',
                type: 'GET',
                data: { id: 1 },
                success: function(response)
                {
                    $('#something').html(response);
                }
            });
       });
    });    
</script>

TestController.php

public function getAjax()
{
    $id = $_GET['id'];
    $test = new TestModel();
    $result = $test->getData($id);

    foreach($result as $row)
    {
        $html =
              '<tr>
                 <td>' . $row->name . '</td>' .
                 '<td>' . $row->address . '</td>' .
                 '<td>' . $row->age . '</td>' .
              '</tr>';
    }
    return $html;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of $id = $_GET['id']; i used Input::get("id") anyways it works thx alot
7

Your ajax's method is GET but in controller you use $_POST to get value. This is problem.

You can you

$id = $_GET['id'];

But in Laravel, it have a pretty method to do this. It's here. You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs.

$id = Input::get("id");

If you want, you can filter request type to control exception. Docs here

Determine If The Request Is Using AJAX

if (Request::ajax())
{
    //
}

2 Comments

Don't I need to specify a route for the url in the $.ajax call?
$.ajax or $.post is the same in this case
0
#in your controller function    
public function getAjax()
{
    #check if request is ajax
    if ($request->ajax()) {
        //your code
    }

    return $your_data;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.