Jquery 输入值为空,而不是输入的值
我想使用JqueryUI自动完成,但当我尝试从搜索输入发送值时,我有一个空值,而不是输入的值。在我的刀片文件中,我有一个表格:Jquery 输入值为空,而不是输入的值,jquery,ajax,laravel,Jquery,Ajax,Laravel,我想使用JqueryUI自动完成,但当我尝试从搜索输入发送值时,我有一个空值,而不是输入的值。在我的刀片文件中,我有一个表格: <form method="post" action="{{ route('meals.store') }}" class="container-form"> @csrf <label for="date">Nazwa produktu:</label> <input name="product-name
<form method="post" action="{{ route('meals.store') }}" class="container-form">
@csrf
<label for="date">Nazwa produktu:</label>
<input name="product-name" type="text" id="product-name" required/>
</form>
此脚本向url“添加/搜索”发送一个术语,该路由与函数搜索相连接:
public function search(Request $request)
{
$name = $request->get('product-name');
Debugbar::info($name);
$result = DB::table('products')->select(array('id', 'name'))->where('name', 'LIKE', '%' . $name . '%')->limit(25)->get();
return response()->json($result->toJson());
}
但我得到了所有结果,因为即使在我输入值时$name变量也是空的
当我尝试使用$request->all()时;我得到了一个包含键和值的数组
“term”=>“ban”
,我不知道当输入名为other时这怎么可能 这是因为您正在发出post请求
并试图获取路由参数
。
改变这个
$name = $request->get('product-name');
到
或
并将产品名称更改为productname
或product\u name
不使用-
使用
或驼峰格
你想做的是需要像下面这样做
http://localhost/meals/store?product-name="abc";
如果您确实喜欢,那么您的代码将工作
$name = $request->get('product-name');
return $name; // will return abc
还可以向您的请求添加验证,如下所示
http://localhost/meals/store?product-name="abc";
控制器:
public function search(Request $request)
{
$this->validate($request, ['product_name' => 'required'])
$name = $request->get('product-name');
Debugbar::info($name);
$result = DB::table('products')->select(array('id', 'name'))->where('name', 'LIKE', '%' . $name . '%')->limit(25)->get();
return response()->json($result->toJson());
}
<form method="post" action="{{ route('meals.store') }}" class="container-form">
@csrf
<label for="date">Nazwa produktu:</label>
<input name="product-name" type="text" id="product-name" required/>
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</form>
刀片文件:
public function search(Request $request)
{
$this->validate($request, ['product_name' => 'required'])
$name = $request->get('product-name');
Debugbar::info($name);
$result = DB::table('products')->select(array('id', 'name'))->where('name', 'LIKE', '%' . $name . '%')->limit(25)->get();
return response()->json($result->toJson());
}
<form method="post" action="{{ route('meals.store') }}" class="container-form">
@csrf
<label for="date">Nazwa produktu:</label>
<input name="product-name" type="text" id="product-name" required/>
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</form>
致:
或者,您也可以将控制器中的$request->product\u name
更改为$request->term
希望能有帮助
谢谢这是因为您正在发出post请求并试图获取参数。更改此$name=$request->get('product-name');要
$request->'product\u name
或$request->only('product\u name')
,还要将product name更改为procductName或product\u name不使用-use或camel case,这两种解决方案都不起作用。我已将输入的名称更改为productName。当我尝试使用$request->'productName'时,我会像以前一样得到null,而仅使用('productName')会得到空数组。仅使用您的请求return$request->all()
并将其输出添加到您的问题添加到该问题。当我将一个键名更改为有效的术语时,我想知道为什么键是term而不是productName?如果您在jquery脚本中看到,您将数据作为数据:{term:request.term}
在此处将term更改为product\u name,或者后端将product name更改为term。
data: {
term: request.term
},
data: {
product_name: request.term
},