Ramzan Mahmood
Ramzan Mahmood

Reputation: 1901

How get id off button in laravel controller laravel 5.3

Here i have buttons like this

<button type="submit" name="submit_1" class="btn green" value="submit_1">Save & Add Another</button>
<button type="submit" name="submit_1" class="btn green" value="submit_2">Save & Exit</button>

in my controller i redirect pages on button click i cannot change name of buttons because i am using in update function to redirect with submit_1 now what should do i also can use id maybe for this but dont know how to use it

if(Input::get('submit_1')) {
        return redirect()->route('data.create');
    } elseif(Input::get('')) {
        return redirect()->route('data.index');
    }

Its always redirect to create page but i want if i click button2 which is Save & Exit it should be redirect to index page. what should i do to to differentiate between buttons except changing name of buttons.. if any method with id please help me and guide to best

Upvotes: 0

Views: 1746

Answers (3)

Gayan Kavirathne
Gayan Kavirathne

Reputation: 3237

first set id in your button. lets say id 1 and id 2 In this case route call in your button should be like..

<a href="[your route]" id=1 class="btn green">

And in your routs it should be like

Route::get('[your link to be displayed]{id?}', 'Controller@action')

In th function call it should be like

 public function action($id = null) {

   if($id=1) {
       return redirect()->route('data.create');
 } 
   elseif($id=2) {
    return redirect()->route('data.index');
}

}

Hope this would be usefull

Upvotes: 0

Serdar Saygılı
Serdar Saygılı

Reputation: 471

You should look for the value of the submit_1 button.

if(Input::get('submit_1') == "submit_1") {
    return redirect()->route('data.create');
} else if(Input::get('submit_1') == "submit_2") {
    return redirect()->route('data.index');
}

However giving same name to 2 different buttons is not a good approach.

Upvotes: 0

Rwd
Rwd

Reputation: 35170

You need to compare the input with a value.

At the minute your basically just saying if('submit_1') (or 'submit_2') either will evaluate to being true, you're not actually checking the the value for submit_1 is actually 'submit_1'.

(I think the confusion comes from having the value of one of your buttons being the same as the name of your buttons )

Change your if condition to be:

if(Input::get('submit_1') == 'submit_1')

Hope this helps!

Upvotes: 2

Related Questions