Using Bootstrap Typeahead in Laravel-5.1

纵然是瞬间 提交于 2019-12-25 11:52:54

问题


I'm trying to do the implementation of this pure PHP project (Re-write pure PHP code in Laravel-5.1 format) in Laravel-5.1 version, and I'm yet to get what needs to be done right. Bootstrap Typeahead is not effected. I would appreciate being pointed to what I'm to do right.

This is the blade form:

{!! Form::open(array('url' => 'created-products', 'method' => 'post'), array('id'=>'createproduct')) !!}
    {!! Form::token() !!}

        <link href="{!! asset('bootstrap/css/bootstrap.css') !!}" media="all" rel="stylesheet" type="text/css" />
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

    <p>{!! Form::select('countries', array('-1' => 'Select a Country') + $listedCountries) !!} </p>
    <p>{!! Form::text('typeahead', null, array('class'=>'well', 'placeholder'=>'enter your state'), array('id'=>'selectedCompanyHiddenId', 'data-provider'=>'typeahead')) !!}</p>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="{!! asset('bootstrap/js/bootstrap.js') !!}" media="all" rel="stylesheet" />

        <script>
            $(function(){
                    $('#typeahead').typeahead({
                        source: function(query, process){
                            $.ajax({
                                url: 'UserAddressController.php?SelectLocationPlaces',
                                type: 'POST',
                                data: 'query=' +query,
                                dataType: 'JSON',
                                async: true,
                                success: function(data){
                                    process(data);
                                }
                            });
                        }
                    });
                });
        </script>

    {!! Form::close() !!}

This is the controller:

public function create()
    {
        $listedCountries = $this->showListedCountries();
        return view('useraddress.create')
        ->with('listedCountries', $listedCountries)
        ->with('returnedLocationPlaces', $this->SelectLocationPlaces());
    }

The method returning the records:

public function SelectLocationPlaces()
{
    if(isset($_POST['query']))
    {
        $query = $_POST['query'];
        $locationPlaces = LocationPlaces::where('name', 'like', "%{$query}%")->toArray();
        return json_encode($locationPlaces);
    }
}

来源:https://stackoverflow.com/questions/34679066/using-bootstrap-typeahead-in-laravel-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!