简体   繁体   English

选择onChange Ajax从MySQL数据库Laravel 5.4获取数据列表

[英]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. 需要根据第一输入字段选择值来填充第二选择输入字段。

Below is my code 下面是我的代码

<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>

Javascript Java脚本

<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>

Controller 控制者

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条件进行比较,最后返回数据的数组结果。

Edited code 编辑代码

$("#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: 这里的两个/三个问题:

  1. You haven't defined $id in the context of your method. 您尚未在方法的上下文中定义$id Access it via your request object $request->input('id'); 通过您的请求对象$request->input('id');访问它$request->input('id');
  2. Don't send your data as a string, send it as an object data: {id: id} 不要将数据作为字符串发送,而应将其作为对象data: {id: id}发送data: {id: id}
  3. Always return a response. 始终返回响应。 If the statement evaluates to false, the request will not be answered. 如果该语句的计算结果为false,则不会响应该请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM