nischalinn
nischalinn

Reputation: 1175

passing data from form to search method of controller

I 've a search box where user will input the name of the users and the name will be searched in database data and if found display in tabular form. But the value is not passed to the search method from the search page.

view page block

<form class="form-horizontal" method="POST" action="{{action('UserController@search')}}">
                    {{ csrf_field() }}
            <div class="row" style="padding-left: 1%;">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>User Name</label><span class="required">*</span>
                            <input type="text" maxlength="100" minlength="3" autofocus="autofocus" autocomplete="off" required="required" name="UserName" class="form-control"/>
                        </div>
                        <div class="form-group" style="padding-left: 5%;">
                            <button type="submit" class="btn btn-primary">Submit</button>        
                        </div> 
                    </div>                       
            </div>
    </form>

controller code block

public function searchDev()
    {
        return view ( 'pages.UserSearch');
    }

public function search(Request $request)
{
    $UserName = $request->input['UserName'];

    return response()->json($UserName);
    return response()->json('hello world');

    if($UserName != ""){
        $User = User::where ( 'NAME', 'LIKE', '%' . $UserName . '%' )->get (['id','NAME','CONTACT','TEMP_ADDRESS']);
        if (count ( $user ) > 0)
        {
            return view ( 'pages.UserSearch' ,compact('User'));
        }  
    }
}

In search method the json response is blank.

route code

Route::get('/UserSearch','UserController@searchDev');
Route::post('/UserSearch','UserController@search');

table structure image enter image description here

Upvotes: 0

Views: 817

Answers (2)

Vision Coderz
Vision Coderz

Reputation: 8078

Retrieving An Input Value Using a few simple methods, you may access all of the user input from your Illuminate\Http\Request instance without worrying about which HTTP verb was used for the request. Regardless of the HTTP verb, the input method may be used to retrieve user input:

$UserName = $request->input('UserName');

or

$UserName = $request->UserName;

Refer https://laravel.com/docs/5.6/requests#retrieving-input

Upvotes: 1

Rwd
Rwd

Reputation: 35180

On the request object, input is a method not an array.

To access the form data you should do:

$request->input('UserName');

For more information on retrieving retrieving input from the request object please refer to the documentation

Upvotes: 1

Related Questions