Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vandyczech's avatar

Checkbox checked problem

I have the checkbox in my blade search template like this:

{!! Form::checkbox('valve', 'on')  !!}

But the checked is not working. I need create the if statement. The statement do something when checkbox will be checked. But this isset construction not working ....

Dump from $request is:

array:2 [▼
  "_token" => "f6Pf5Dbv4jvGRoqi7kGBiS5m9ZVDfwu4OdFXarhg"
  "valve" => "on"
]

Statement is:

if( isset($request->valve) === "on" ){ ...do something }
0 likes
20 replies
Snapey's avatar

if( isset($request->valve) === "on" )

is not valid

isset($request->valve) evaluates to true or false.

Therefore you are saying true or false === "on"

Try if($request->valve === "on" )

Snapey's avatar

By the way, checkboxes that are not checked are not returned in a form response.

You can just check for the existence of the field.

vandyczech's avatar

I try this, but doesnt work ...

if( array_key_exists('valve', $request->all()) ){}
Snapey's avatar

What? I'm sure I gave the answer?

If you want to check if it is set then if($request->valve) should work.

vandyczech's avatar

but when I used, show:

MethodNotAllowedHttpException
Snapey's avatar

that's nothing to do with this bit of code

vandyczech's avatar
in RouteCollection.php line 233
at RouteCollection->methodNotAllowed(array('POST')) in  RouteCollection.php line 220
at RouteCollection->getRouteForMethods(object(Request),     array('POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 533
vandyczech's avatar

The problem is, that the form is in index.blade.php and this file include search-bar with search input ... route path focus on URL /search ... so when the user search something from url / the form route path is /search ... but search.blade.php include search-bar too ... so when user searching from url /search ... the form is submitted to the /search again... and this is the problem I mean ...

vandyczech's avatar

I used Laravel 5.4, Laravelcollective 5.3. Problem is, that the user searching from URL /search ... form is submitted to /search again ... and this is show error MethodNotAllowed. And the $request->search not contains value ... so SQL show error ....

My form is:

{!! Form::open(['route' => 'contact.search']) !!}

<input type="text" name="search" class="form-control" placeholder="Search...">
<button type="submit" class="btn btn-success"><i class="fa fa-search" aria-hidden="true"></i> Search</button>

{!! Form::checkbox('valve', 'on')  !!}
{!! Form::close() !!}

My controller is:

public function search( Request $request ){

    if($request->valve){

    $result = DB::select("SELECT * FROM personals 
                              LEFT JOIN functions ON personals.funkce = functions.id 
                              LEFT JOIN departments ON personals.oddeleni = departments.id 
                              WHERE klapka = $request->search");

    }

return view('contacts.search')->withResults($result)


}
vipin93's avatar
<form method="get">

<input type="text" name="search" class="form-control" placeholder="Search...">
<button type="submit" class="btn btn-success">
<i class="fa fa-search" aria-hidden="true"></i> Search</button>
<input type="checkbox" name="on"> on 
</form>

  if($request->valve){

    $result = DB::select("SELECT * FROM personals 
                              LEFT JOIN functions ON personals.funkce = functions.id 
                              LEFT JOIN departments ON personals.oddeleni = departments.id 
                              WHERE klapka = $request->search");//here u using where it means if $request is not match the yor klapka  it give nothing so here if want search may be empty so use "Orwhere", u can use Elaqaut which very easy 

    }
sylar's avatar

By default, a POST method will be assumed try this

Form::open(['route' => 'contact.search', 'metohd' => 'get' ])
vandyczech's avatar

its not working ... Please read again ...

index.blade.php contains include search-bar.blade.php, search.blade include search-bar.blade.php too.

From index call function index(), return view('contacts.index') From search call function search(), return view('contacts.search')

Route is

Route::post('/search'      , 'ContactController@search') ->name('contact.search');
vandyczech's avatar

The problem is, that user search from url / .... index() form is submitted via POST method to the url /search ... but when user again search .. searched from /search no / .. so form is submitted to /search ... this I need fix it ... another i see MethodNotAllowed

vandyczech's avatar

I dont know, I need the search form still via top of the page ... in each page ..

Snapey's avatar

What does your routes/web.php look like?

vandyczech's avatar
Route::get('/' , 'ContactController@index')  ->name('contact.index');
Route::post('/search' , 'ContactController@searchresults') ->name('contact.search');
Snapey's avatar

Create a new partial and put this in it;

    <form method="post", action="{{ route('contact.search') }}" >
        {{ csrf_field() }}
        <input type="text" name="search" class="form-control" placeholder="Search...">
        <button type="submit" class="btn btn-success"><i class="fa fa-search" aria-hidden="true"></i> Search</button>
        <input type="checkbox" name="valve "value="on" />
    </form>

Then include the partial in your master layout.

You should now have the form at the top of every page that uses the master layout

You can then fill in the form and press submit. It will follow your route and go to your ContactController with $request->search and $request->valve passed over.

You then do the search and render the search results.

Please or to participate in this conversation.