jeudi 27 août 2015

make a post request from jquery to laravel controller

I'm trying to make a post request to a Laravel Controller, but when i send the data through ajax it doesn't work, here are my scripts:

Here's the form:

<form >
    <label for = "cantidadpersonas" >Cantidad de personas</label >
    <input id="personas" type = "text" placeholder="#" >
    <br ><br ><br >

    <label for = "comidas" >Seleccione los platillos que desea reservar</label >
    <br ><br ><br >
    <div id="comidas" class="owl-carousel owl-theme">

        @foreach($platos as $plato)
            <a class="item opcion comida">
                <p class="hidden">{{ $plato->id }}</p>
                <img id="{{ $plato->id }}" class="img" src = "{{ asset('club/img/gallery/gallery1.jpg') }}" alt = "{{ $plato->nombre }}" >
            </a >
        @endforeach
    </div>
    <br ><br ><br >
    <div id="bebidas" class="owl-carousel owl-theme">
        @foreach($bebidas as $bebida)
            <a class="item opcion bebida">
                <p class="hidden">{{ $bebida->id }}</p>
                <img id="{{ $plato->id }}" class="img" src = "{{ asset('club/img/gallery/gallery2.jpg') }}" alt = "{{ $bebida->nombre }}" >
            </a >
        @endforeach
    </div>
    <br ><br >

    <button id = "enviar" >Siguiente</button >
    <br ><br ><br >
    <!-- /input-container -->
</form >

Here's the post function in laravel controller:

public function pasounoPost(){

        dd(\Input::all());

        if(\Request::ajax()) {
            $data = \Input::all();
            dd($data);
        }

    }

And here's my ajax code for click event on enviar button:

$("#enviar").click(function(){
        event.preventDefault();

        var bebidas = $('.clicked.bebida').map(function () {
            var $this = $(this);
            var bebida = {};
            bebida.id = $this.find('p.hidden').text();
            return { bebida: bebida};
        }).get();

        var comidas = $('.clicked.comida').map(function () {
            var $this = $(this);
            var bebida = {};
            bebida.id = $this.find('p.hidden').text();
            return { bebida: bebida};
        }).get();

        var bebidas_json = JSON.stringify(bebidas);
        var comidas_json = JSON.stringify(bebidas);

        $.ajax( {
            type:  'post',
            url  : '/reservacion/paso-uno/enviar',
            data : { bebidas: bebidas_json },
            success:  function () {
                console.log('Hecho');
                console.log(bebidas_json);
            }
            //dataType: "json"
        } );

    });

And here are my routes:

Route::get('/reservacion/paso-uno', [
        'as' => 'publicReservacionPasoUno',
        'uses' => 'ReservacionCtrl@pasouno'
    ]);

    Route::post('/reservacion/paso-uno/enviar', [
        'as' => 'publicPostReservacionPasoUno',
        'uses' => 'ReservacionCtrl@pasounoPost'
    ]);

I get the success function when i click the button, but i don't get redirected to the next view... that is declared in the laravel controller.

What i need is to send data to the pasounoPost function from jquery ajax, and redirect to another route and send the data sent from ajax.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1JmpA6z
via IFTTT

Aucun commentaire:

Enregistrer un commentaire