Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if form submitted in laravel?

Tags:

laravel

I am trying to check if the form submit button is clicked so I tried:

if($request->input('submit')){
    //do something
    }

and tried:

$clicked=$request->input('submit');
if(isset($clicked)){
    //do something
    }

and also tried:

if($request->input('submit')!=null){
    //do something
    }

but when I do click the submit button in the form the execution of if-inside never happens so what is the right way to check if submit button clicked in laravel?

like image 879
mark Avatar asked Mar 06 '23 15:03

mark


2 Answers

Check if the value of submit button is set. This might be the easiest also, I always use it.

HTML:

<button type="submit" name="find" value="Find">Find</button>

LARAVEL:

if (isset($request->find)) 
{
    //code
}
like image 92
RRR Avatar answered Mar 15 '23 12:03

RRR


this can be solved two ways: first way if your form contains just one button and your form is using POST then like Adnan Mumtaz said we can use :

if($request->method() == 'POST'){
//ur code here
}

but if the form contains multiple buttons and we want check if button than have name btn1 clicked then we can use:

if($request->has('btn1')){
//rest of the  code here
}
like image 28
mark Avatar answered Mar 15 '23 12:03

mark