douqiao3930
2018-01-03 05:55
浏览 63
已采纳

在laravel中使用Ajax自动完成表单

I trying to autofill my form but everything is okay data return from my database table but my form does not fill auto please suggest me what will I do now.I am uploading all my code.This is form and js code

<div class="col-md-7">
    <div class="form-group">
        {{Form::label('reg_id','Student Registration Number')}}
        {!! Form::text('reg_id', null, array('id'=>'reg_id','placeholder' => 'Enter Student Registration Number','class' => 'form-control')) !!}
    </div>
    <div class="form-group">
        {{Form::label('Name','Student Name')}}
        {!! Form::text('name', null, array('id'=>'name','placeholder' => 'Enter Student Name','class' => 'form-control')) !!}
    </div>
    <div class="form-group">
        {{Form::label('Email','Student Email')}}
        {!! Form::email('email', null, array('id'=>'email','placeholder' => 'Enter Student Email','class' => 'form-control')) !!}
    </div>

    <div class="form-group">
        {{Form::label('Department','Department ')}}
        {{csrf_field()}}
        <select name="department" class="form-control" id=>'department_id'>
            <option value=" ">----Select Department-----</option>
            {{--@foreach($department as $value)
            <option value="{{$value->id}}">{{$value->name}}</option>
            @endforeach--}}
        </select>
    </div>
</div>

//jQuery code

$('#reg_id').autocomplete({
    source : '{!!URL::route('autocomplete')!!}',
    minlenght:3,
    autoFocus:true,
    select:function(event,ui){
        $('#reg_id').val(ui.item.value);
    }
});

//Here is the controller code:

public function autocomplete(Request $request)
{
    $term=$request->term;
    $data = Student::where('reg_id','LIKE','%'.$term.'%')->with('department')
        ->take(10)
        ->get();
    $result=array();
    foreach ($data as $key => $v){
        $result[]=['reg_id' =>$v->reg_id,'name'=>$v->name,'email'=>$v->email,'department_id'=>$v->department_id];
    }
    return response()->json($result);
}

//And Route code also:

Route::get("/autocomplete",array('as'=>'autocomplete','uses'=> 'EnrollCourseController@autocomplete'));
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题