Koen van de Sande
Koen van de Sande

Reputation: 223

Laravel 5: Request data is null when using an Ajax Post Request

I have some problems with receiving data when using an ajax call to EDIT my data. I'm trying to get data that the user has filled in but every time my data is empty or 'null'. Do i need to pass data in ajax call? Or are there other ways in laravel to get the data?

My code at the moment as we speak:
showuser.blade.php

    <div class="comments">
    @if(count($user->cars))
        <h1>Auto in reparatie</h1>
        <hr>
        @foreach($user->cars as $car)
        <!-- Form to show each car and edit / delete the cars -->
            <!--<form action="{{ action('CarController@edit', [$user->id, $car->id]) }}" method="POST">-->
            <form>
                <div class="form-group">
                    <label for="brand">Merk:</label><br>
                    <select name="brand">
                        @foreach($brands as $brand)
                            <option value="{{$brand->id}}"{{ $brand->id == $car->brand_id ? ' selected="selected"' : '' }}>{{$brand->brandname}}</option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <label for="year">Bouwjaar:</label><br>
                    <input type="text" value="{{$car->year}}" name="year">
                </div>
                <div class="form-group">
                    <label for="licentsplate">Kenteken:</label><br>
                    <input type="text" value="{{$car->licensplate}}" name="licenseplate">
                </div>
                <div style="float: left; width: 80px">
                    <input type="submit" class="btn btn-primary" value="Edit" id="{{$car->id}}" name="editcar" />
                </div>
            </form>
            <form>
                <div class="form-group">
                    <button class="btn btn-primary" name="deletecar" id="{{$car->id}}">Delete</button>
                </div>
            </form>
        @endforeach
    @endif
</div>

Script

<script type="text/javascript">
    $( document ).ready(function() {
        $('[name="editcar"]').click(function (e){
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: '/users/{{$user->id}}/cars/'+this.id,
                success:function(data){
                    if(!data.error){
                      location.reload(true);
                    }
                }
            })
        });
    });
</script>

Routes

 Route::post('/users/{userId}/cars/{carId}', 'CarController@edit');

CarController.php

    // Function to edit a car by ID
public function edit($userId, $carId)
{
    $car = Car::where('id', $carId)->where('user_id', $userId)->first();
    $car->brand_id = request('brand');
    dd(request('year'));
    $car->year = request('year');
    $car->licensplate = ('licenseplate');
    $car->save();
}

If I dump and die request('brand'), request('year'), request('licenseplate') i get 'null'.

EDIT: If I dd $car in my controller, i get the car that i need. Means that there is no problemen finding the right car.

Thx for reading!

Upvotes: 1

Views: 4562

Answers (3)

mbozwood
mbozwood

Reputation: 1402

You aren't posting any data in the AJAX request. Pass the form values through in an object.

var form = $(this).find('form');
var licenseval = form.find('input[name=licenseplate]').val();
var yearval = form.find('input[name=year]').val();
var brandval = form.find('select[name=brand]').find(":selected").val();
$.ajax({
    type: "POST",
    data: {licenseplate: licenseval, year: yearval, brand: brandval}
    url: '/users/{{$user->id}}/cars/'+this.id,
    success:function(data){
        if(!data.error){
            location.reload(true);
        }
    }
});

Upvotes: 2

Vaibhavraj Roham
Vaibhavraj Roham

Reputation: 1165

As per your ajax call you are only calling the route

Route::post('/users/{userId}/cars/{carId}', 'CarController@edit');

but you are not passing any other value. Your application is working as expected.

$.ajax({
        type: "POST",
        url: '/users/{{$user->id}}/cars/'+this.id,
        //data: dataObject, ----->Missing Data which you want to pass (brand, year, licenceplate)
        success:function(data){
            //...
        }
    })

You are iterating through multiple $cars you need to get current car's data and pass to ajax call.

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Change following lines:

public function edit($userId, $carId)

to

public function edit(Request $request, $userId, $carId)

and use

use Illuminate\Http\Request;

after that you can get $userId, $carId to use inside the controller function.

Upvotes: 0

Related Questions