简体   繁体   English

在 Laravel 或 PHP 的搜索字段中显示 Json

[英]Display Json in search field in laravel or Php

I need to display searched value on search bar.我需要在搜索栏上显示搜索值。

My controller code :我的控制器代码:

$q = Input::get('q');
  $query = Jobs::select('job_title')->Where('job_title', 'LIKE', '%'.$q.'%')->get();
  return response()->json($query);

My jquery axios code :我的 jquery axios 代码:

 $(document).ready(function(){
  $("#jobsearch").keyup(function(){
     url = "{{route('searchexistingjob')}}";
     var word = $("#jobsearch").val();
     const data = {
       'q' : word
     }
     axios.post(url,data).then(response =>{
        for(i=0; i<response.data.length; i++){
            // $('#jobsearch').val(response.data[i].value);           
     });
  });
});

HTML code HTML代码

<input type="text" id="txtjobsearch" name="txtjobsearch" class="form-control" placeholder="Job title, designation, description..." autocomplete="off" data-id="1">

all the thing are correct only but i need how to display that value in search box any ideas?所有的事情都是正确的,但我需要如何在搜索框中显示该值有什么想法吗?

If can you can change the response format如果可以,您可以更改响应格式

$q = Input::get('q');
$query = Jobs::select('job_title')->Where('job_title', 'LIKE', '%'.$q.'%')->get();
return response()->json([
    search_terms => $q,
    results => $query,
]);

Or as you are using ajax, you already have the search term:或者当您使用 ajax 时,您已经有了搜索词:

So instead of:所以而不是:

$('#jobsearch').val(response.data[i].value);  

You will use:您将使用:

$('#jobsearch').val(word);  

Or just remove the line and leave what is already typed.或者只是删除该行并保留已键入的内容。

You can do it in a few easy steps.您可以通过几个简单的步骤来完成。

Make a route, with same link, controller and function, just use POST method.创建一个路由,使用相同的链接、控制器和功能,只需使用 POST 方法。 Wrap input field in the form, and when its submitted, let it go to the same page.将输入框包裹在表单中,提交后跳转到同一个页面。 Create the variable in that controller@function, let's say:在该控制器@函数中创建变量,让我们说:

$result = ""; $result = "";

And pass that variable to the view.并将该变量传递给视图。 And in the view, set the value of that input to the $result;在视图中,将该输入的值设置为 $result;

If its not queried yet, that input will be clear($result = '';), otherwise, it will display result of that query.如果尚未查询,则该输入将是 clear($result = '';),否则,它将显示该查询的结果。

// routes/web.php 
Route::get('/home', 'SearchBarController@index');
Route::post('/home', 'SearchBarController@index');

// app/Http/Controllers/SearchBarController.php
public function index()
{
    $result = '';
    if(request()->isMethod('post'))
    {
        $q = Input::get('textjobsearch');
        $query = Jobs::select('job_title')->where('job_title', 'LIKE', '%'.$q.'%')->get();
        $result = json_encode($query);
    }

    return view('searchbar.index', compact('result'));
}

// resources/views/searchbar/index.blade.php

<form method='post'>
    <input type="text" id="txtjobsearch" name="txtjobsearch" value="{{ $result }}" class="form-control" placeholder="Job title, designation, description..." autocomplete="off" data-id="1">
<input type='submit' />
</form>

That's it.而已。

That simple.就那么简单。

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

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