doucang6739
2016-10-10 03:33
浏览 28

Laravel AJAX表格(接收数据关注)

I have an issue recieving the data from a form which was modified with AJAX.

I have 2 fields: Cinema and Session.

The Session field updates automatically based on which cinema you have chosen. However, when I try to get the session field's value in my controller, it returns as "" (blank) where-as I want it to return what is actually displayed in the drop down box in the form.

Code:

Script:

<script>
$('#cinema').on('change', function(e){
    console.log(e)
    var cinema_id = e.target.value;


//ajax
    $.get('{{ url('/ajax-subcat') }}?cinema_id=' + cinema_id, function(data){
    //success data
    console.log(data)
    $('#sesh').empty();
    $.each(data, function(index, subcatObj){
        $('#sesh').append('<option value="'+subcatObj.id+'">'+subcatObj.session_time+'</option>');;
     });
});
});
</script>

Controller:

public function cart()
{
    $formData = array(
        'cinema'  => Input::get('cinema'),
        'sesh'  => Input::get('sesh'),
        'ticketType' => Input::get('ticketType'),
        'count' => Input::get('count')
    );
    $cinemaDetails = Cinema::find($formData['cinema']);
    $session = Session::find($formData['sesh']);
    return view('movies/ticketpage/cart')
        ->with('formData', $formData)
        ->with('cinemaDetails', $cinemaDetails)
        ->with('session', $session);
}

View/Blade file:

<div class="container">
    <div class="row">
        <div class="col-sm-4">
            <h2>{{$movie->title}}</h2>
            <img src="/WDAAssign2/Assign2-A/{{ $movie->image }}" height="200" width="150">
            <p>{{ $movie->description }}</p>
        </div>
        <div class="col-sm-4">
        <h2>Purchase tickets!</h2>
        {!! Form::open(array('action'=>'MoviePageController@cart', 'files'=>true)) !!}
                <label>Select a Cinema:</label><br>
                <select id = "cinema" name = "cinema">
                    @foreach ($cinemas as $cinema)
                        <option value="{{$cinema->id}}">{{$cinema->name}}</option>
                    @endforeach
                </select>
                <br>
                <label>Select a session:</label>
                <br>
                <select id = "sesh" name = "sesh">
                    <option value=""></option>
                </select>
                <br>
                <label>Type of ticket:</label>
                <br>
                <select id= ="ticketType">
                    <option value="adult">Adult</option>
                    <option value="concession">Concession</option>
                    <option value="children">Children</option>
                </select>

                <br>
                <label>Number of tickets:</label><br>
                <select id = "count" name ="count">
                    @for ($i = 1; $i < 10; $i++)
                        <option value="{{$i}}">{{$i}}</option>
                    @endfor
                </select>
                <br><br>
                <input type="submit" value="Submit">
            {!!Form::close()!!}
        </div>
    </div>
</div>

Routes:

Route::post('/movie/ticketpage/cart', 'MoviePageController@cart');
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

相关推荐 更多相似问题