Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 输入值为空,而不是输入的值_Jquery_Ajax_Laravel - Fatal编程技术网

Jquery 输入值为空,而不是输入的值

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

我想使用JqueryUI自动完成,但当我尝试从搜索输入发送值时,我有一个空值,而不是输入的值。在我的刀片文件中,我有一个表格:

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