0

I am trying to show data which is passed from controller to blade and manipulate something with it in my view page. But I can see the my data passed correctly in my console but i am not getting it in my view file. I am using Laravel 4

Controller:

public function addRow()
{
    if(Request::ajax()){
        $row = Input::all();        
    }
    return View::make('add-stock')
        ->with('rows', $row);
}

View :

@if( !empty($rows) )
    @foreach($rows as $row)
        {{ $row[0]  }}
    @endforeach
@endif

route:

Route::post('add-stock/row','StockController@addRow');

jQuery :

<script>
    $(document).ready(function(){

        $('.rowForm').click(function(e){
            e.preventDefault();

            var rowVal = $('input[name=row]').val();

            //ajax post
            $.post('add-stock/row', {row:rowVal}, function(data){
                console.log(data);
            })
        })          


    })
</script>

2 Answers 2

1

I would do like this. Your ajax function........

public function addRow(){  

    $row = Input::all();

    return Response::json($row);
 }

And then your JQuery............

<script>
$(document).ready(function(){

    $('.rowForm').click(function(e){
        e.preventDefault();

        var rowVal = $('input[name=row]').val();

        //ajax post
        $.get('/add-stock/row?rowVal =' +rowVal , function(data){
            console.log(data);
             $('#rowVal ').empty();
        $.each(data,function(index,subcatObj)
        {
            $('#rowVal ').append('<option value="'+subcatObj.value+'">'+subcatObj.value+'</option>');
        })
        })
    })          


})

here #rowVal id of the div or select attribute

And atlast ...........the route file

Route::get('add-stock/row','StockController@addRow');

By the way you didn't specify from where JQuery will be called

Sign up to request clarification or add additional context in comments.

Comments

0

You can return the response with JSON content type. Luckily, Laravel provides a tool for that. You could do something right this

if(Request::ajax()){
        return Response::json(Input::all()); 
}

for more information: http://laravel.com/docs/4.2/responses#special-responses

5 Comments

how can i return the value to my desired view ??
Do you get anything at "console.log(data);"?
no it shows -- {"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to undefined function response()","file":"C:\\xampp\\htdocs\\easybiz\\app\\controllers\\StockController.php","line":93}}
Are you using Laraval 4 or 5?
laravel.com/docs/4.2/responses#special-responses . Using the code in the link instead.

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.