[英]Select onChange Ajax get list of data from mysql database Laravel 5.4
I have 2 select input fields. 我有2个选择输入字段。
first select input field contains static data. 首先选择输入字段包含静态数据。
second select input field needs to be populated depending on first input field selected value. 需要根据第一输入字段选择值来填充第二选择输入字段。
<select id="filter_type" name="filter_type" class="selectpicker form-control" style="width:30%;">
<option value>Select Filter Type..</option>
<option value="abc">Abc</option>
<option value="def">Def</option>
<option value="ghi">Ghi</option>
<option value="jkl">Jkl</option>
</select>
<select id="search" name="search" class="selectpicker form-control" style="width:70%;" disabled="disabled">
<option value>Select Filter type first..</option>
</select>
<script type="text/javascript">
$(document).ready(function()
{
$("#filter_type").change(function()
{
var id=$(this).val();
console.log(id);
var dataString = 'id='+ id;
console.log(dataString);
$.ajax
({
type: "GET",
url: "{{url('/users/abc')}}",
data: dataString,
cache: false,
success: function(data)
{
console.log(data); // I get error and success function does not execute
}
});
});
});
</script>
public function get_onchange_data(Request $request)
{
if($id == 'abc')
{
$searches = Abc::select('field_name')->get();
return response(view('/users/Def', compact('searches'))->render());
}
}
I am unable to get returned ajax data. 我无法获取返回的ajax数据。
I get this error: 我收到此错误:
GET http://localhost:8000/users/abc?id=year&_=1496176229318 500 (Internal Server Error)
I think my check in if condition is wrong. 我认为如果条件不正确,我应该检查一下。 I am considering $id as the value passed from dataString.
我正在考虑$ id作为从dataString传递的值。
I don't know how to do this. 我不知道该怎么做。 Get the data, compare it with if condition and finally return array result of the data.
获取数据,将其与if条件进行比较,最后返回数据的数组结果。
$("#filter_type").change(function()
{
var id=$(this).val();
console.log("1:"+id);
var dataString = '{id:'+ id+'}';
console.log("2:"+dataString);
$.ajax
({
type: "GET",
url: "{{url('/users/abc')}}",
data: dataString,
cache: false,
success: function(data)
{
console.log("3:"+data);
}
});
});
public function get_search_data(Request $request)
{
return response($request->input('id'));
}
Two/Three issues here: 这里的两个/三个问题:
$id
in the context of your method. $id
。 Access it via your request object $request->input('id');
$request->input('id');
访问它$request->input('id');
data: {id: id}
data: {id: id}
发送data: {id: id}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.