我是Laravel框架的新手。我在Laravel 5.1工作。我的问题非常简单:
我在表单中有一个动态选择框,其中包含ID 1,2,3,...
后面跟着一些文字框,如姓名,地址,联系方式等。
我想在选择相应的ID时自动填充数据库中的文本框。
我会非常感激,如果一个人可以分享我必须在视图中写的代码&控制器。
如果可能,请建议任何相关教程。
由于
查看:
<input type="hidden" name="_token" value="{!! csrf_token() !!}" id="_token" >
<label for="reqid" class="col-lg-2 control-label">* Permission Requester</label>
<div class="col-lg-10" >
<select class="form-control" name="permission-requester" id="permission-requester" onchange= "getvisitorinfo(this.value)" >
<option disabled selected></option>
@foreach($permissionrequesters as $permissionrequester)
<option value="{{$permissionrequester->id}}" @if (old('permission-requester')==$permissionrequester->name) selected="selected" @endif>{{$permissionrequester->name}}</option>
@endforeach
</select>
</div>
<div class="col-xs-push-12">
<table class="table" border="1" >
<thead>
<tr><td colspan="6" style="background-color: #009d8d "> <font style="color: #ffffff">Permission Requester Details</font></td> </tr>
</thead>
<tbody>
<tr>
<th>Name</th>
<td> <input type="text" class="form-control" value="{{Input::old('requester-name')}}" id="reqname" name="reqname"> </td>
<th>Relation</th>
<td><input type="text" class="form-control" value="{{Input::old('requester-relation')}}" id="reqrelation" name="reqrelation"> </td>
<th>Contact No.</th>
<td><input type="text" class="form-control" value="{{Input::old('requester-contact')}}" id="requester-contact" name="requester-contact"> </td>
</tr>
<tr>
<th>Occupation</th>
<td> <input type="text" class="form-control" value="{{Input::old('requester-occupation')}}" id="requester-occupation" name="requester-occupation"> </td>
<th>Address</th>
<td colspan="3"><input type="text" class="form-control" value="{{Input::old('requester-address')}}" id="requester-address" name="requester-address"> </td>
</tr>
</tbody>
</table>
</div>
&#13;
JS:
<script>
function getvisitorinfo(visitor_id){
var formData = {
'visitor_id':visitor_id,
'_token':_token
};
$.ajax({
type:'POST',
url: '/visitorinfo',
data:formData,
dataType:'json',
success:function(data){
var res_status = data.status;
var res_message = data.message;
var res_data = data.data; //your DB data
if(res_status == 'success'){
$('#reqname').val(res_data.relation);
$('#reqrelation').val(res_data.relation);
}else if(res_status == 'fail'){
alert('error')
}
},
error:function(){
alert('error')
}
});
}
</script>
&#13;
控制器:studpermitController.php
public function visitorinfo(Request $request)
{
$visitor_id = $request->input('visitor_id');
$visitorinfo = authorise_d::where('id',$visitor_id)->first()->toArray();
if(sizeof($visitorinfo) > 0){
$data = ['status'=>'success','message'=>'Visitor Allowed','data'=>$visitorinfo];
}else{
$data = ['status'=>'fail','message'=>'Not Valid Visitor','data'=>''];
}
return Response::json($data);
}
&#13;
路线
Route::post('/visitorinfo','studpermitController@visitorinfo');
&#13;
答案 0 :(得分:1)
以下是一些示例代码供您理解,我希望它能帮助您解决问题。根据需要调整请求的路由URL和代码。
<强> HTML /视图强>
<select name="state_id" id="state_id" onchange="getStateCourses(this.value)">
<option value="0">All States</option>
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option>
<option value="5">California</option>
<option value="6">Colorado</option>
</select>
<强> JS 强>
function getStateCourses(state_id){
var formData = {
'state_id':state_id,
'_token':_token
};
$.ajax({
type:'POST',
url: adminBaseURL+'/reports/statecourses',
data:formData,
dataType:'json',
success:function(data){
var res_status = data.status;
var res_message = data.message;
var res_data = data.data; //your DB data
if(res_status == 'success'){
$('#name').val(res_data.name);
$('#address').val(res_data.address);
}else if(res_status == 'fail'){
alert('error')
}
},
error:function(){
alert('error')
}
});
}
控制器方法
public function postStateCourses(Request $request){
$state_id = $request->input('state_id');
$courses_list = Courses::where('state_id',$state_id)->first()->toArray();
if(sizeof($courses_list) > 0){
$data = ['status'=>'success','message'=>'State courses found!','data'=>$courses_list];
}else{
$data = ['status'=>'fail','message'=>'State courses not found!','data'=>''];
}
return Response::json($data);
}
答案 1 :(得分:1)
最后我找到了解决方案的方法。非常感谢&#34; Qazi&#34;为了大家的帮助。 只是把我的最终代码(减少到我的要求)完全适合我,并且可能对像我这样的其他人有用:
<强>路线:强>
Route::post('/visitorinfo','studpermitController@visitorinfo');
&#13;
查看(采取部分/关联代码)
<meta name="_token" content="{!! csrf_token() !!}"/>
<label for="reqid" class="col-lg-2 control-label">* Permission Requester</label>
<div class="col-lg-10" >
<select class="form-control" name="permission-requester" id="permission-requester" onchange= "getvisitorinfo(this.value)" >
<option disabled selected></option>
@foreach($permissionrequesters as $permissionrequester)
<option value="{{$permissionrequester->id}}" @if (old('permission-requester')==$permissionrequester->name) selected="selected" @endif>{{$permissionrequester->name}}</option>
@endforeach
</select>
</div>
<div class="col-xs-push-12">
<table class="table" border="1" >
<thead>
<tr><td colspan="6" style="background-color: #009d8d "> <font style="color: #ffffff">Permission Requester Details</font></td> </tr>
</thead>
<tbody>
<tr>
<th>Name</th>
<td> <input type="text" class="form-control" value="{{Input::old('requester-name')}}" id="reqname" name="reqname"> </td>
<th>Relation</th>
<td><input type="text" class="form-control" value="{{Input::old('requester-relation')}}" id="reqrelation" name="reqrelation"> </td>
<th>Contact No.</th>
<td><input type="text" class="form-control" value="{{Input::old('requester-contact')}}" id="requester-contact" name="requester-contact"> </td>
</tr>
<tr>
<th>Occupation</th>
<td> <input type="text" class="form-control" value="{{Input::old('requester-occupation')}}" id="requester-occupation" name="requester-occupation"> </td>
<th>Address</th>
<td colspan="3"><input type="text" class="form-control" value="{{Input::old('requester-address')}}" id="requester-address" name="requester-address"> </td>
</tr>
</tbody>
</table>
</div>
&#13;
<强> studpermitController.php 强>
public function visitorinfo(Request $request)
{
$visitor_id = $request->input('visitor_id');
$visitorinfo = authorise_d::where('id',$visitor_id)->first()->toArray();
return ($visitorinfo);
}
&#13;
<强> JS 强>
function getvisitorinfo(visitor_id){
var route='/visitorinfo';
var data = {'visitor_id':visitor_id,};
$.ajaxSetup({
headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
$.ajax({
type:'POST',
url:route,
data:data,
success:function(data){
$('#reqname').val(data.name);
$('#reqrelation').val(data.relation);
},
error:function(){
alert('error')
}
});
}
</script>
&#13;