vendredi 22 janvier 2016

Load text field using ajax in Lalavel

I am trying to load employee name in a employee text field after selecting employee id from another select field.

Route:

post('driver','SalaryController@driver');

Controller:

public function driver($eid)
{
    $driver = Employee::all()->where('eid',$eid)->pluck('name');
    return $driver;
}

Blade:

{!! Form::open(['action'=>'SalaryController@pay','method'=>'post','class'=>'form-horizontal']) !!}
<div class="col-md-12 d-section">
    <div class="col-md-6"> {{-- Left hand side --}}
        <div class="form-group"> {{-- Date --}}
            {!! Form::label('date','Date:',['class'=>'control-label col-sm-3']) !!}
            <div class="col-sm-9">
                {!! Form::text('date',null,['class'=>'form-control datetimepicker']) !!}
            </div>
        </div>
        <div class="form-group"> {{-- eid --}}
            {!! Form::label('eid','ID:',['class'=>'control-label col-sm-3']) !!}
            <div class="col-sm-9">
                {!! Form::select('eid',$repository->employees(),null,['class'=>'form-control','id'=>'eid']) !!}
            </div>
        </div>
    </div>
    <div class="col-md-6"> {{-- Right hand side --}}
        <div class="form-group"> {{-- Month --}}
            {!! Form::label('month','Month:',['class'=>'control-label col-sm-3']) !!}
            <div class="col-sm-9">
                {!! Form::select('month',$repository->months(),null,['class'=>'form-control']) !!}
            </div>
        </div>
        <div class="form-group"> {{-- name --}}
            {!! Form::label('name','Name:',['class'=>'control-label col-sm-3']) !!}
            <div class="col-sm-9">
                {!! Form::text('name',null,['class'=>'form-control','id'=>'name']) !!}
            </div>
        </div>
        <div class="form-group"> {{-- Paying --}}
            {!! Form::label('paid','Paying',['class'=>'control-label col-sm-3']) !!}
            <div class="col-sm-9">
                {!! Form::text('paid',null,['class'=>'form-control']) !!}
            </div>
        </div>
    </div>
</div>
<div class="col-md-12 d-section text-center">
    {!! Form::submit('PAY',['class'=>'btn btn-success','name'=>'advance']) !!}
    {!! Form::reset('RESET',['class'=>'btn btn-warning']) !!}
    <a href="{{ action('SalaryController@index') }}" role="button" class="btn btn-danger">CANCEL</a>
</div>
{!! Form::close() !!}

Ajax:

<script>
    $(document).ready(function(){
        $('#eid').change(function(){
            var eid = $(this).val();
            var csrf = $('#csrf').val();
            var name = $('#name').val();
            $.ajax({
                URL : 'driver',
                data : {driver:eid,_token:csrf},
                type : 'post'
            }).done(function(e){
                name.html(e)
            })
        })
    })
</script>

The code is not working. I tested with firebug. After select employee id it shows the following error

POST http://localhost/zamzam/advance 500 Internal Server Error 1.05s jquery.min.js (line 5)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire