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

p0t4t0's avatar

How do I handle multiple submit buttons in a single form with Laravel?

I have a form with 3 buttons, one for saving the form data into the database, one for previewing and one for advanced options which should redirect the user to a view with a WYSIWYG editor. I have an idea on how to do this with plain PHP but no clue as to how to handle this in my controller with Laravel.

0 likes
15 replies
Robstar's avatar

Have multiple submit buttons, with the same name, but different values for each. You could run some form of switch or if statement within your form handler class. This isn;t specific to Laravel.

Robstar's avatar

What do you mean by a "Laravel" way? Your question is just simple HTML and basic PHP, all of which Laravel can handle. What code do you have so far?

p0t4t0's avatar
switch ($request['action']) {
        case 'Post Reply':
          echo 'foo';
          break;
        case 'Preview':
          echo 'bar';
          break;
        case 'Go Advanced':
          return view();
          break;
        default:
          # code...
          break;
      }

Right now I am under the impression that placing plain PHP code inside a Laravel app is "messy" since I am not utilizing Laravel's functions. Although this code works, it has a slight problem.

The submit button for 'Go Advanced' needs to be a GET request since it should redirect a user to a completely new page with a WYSIWYG editor. But I did come up with a solution for that, but I'm not sure if it is preferred. My solution is simply removing the type="submit" attribute for that button but still place it inside the form so I can style all 3 buttons side by side. In doing this, I can probably have the 'Go Advanced' button act like just a simple button instead of a submit and route the user to the above mentioned page. What do you think?

martinbean's avatar
Level 80

@p0t4t0 Laravel is PHP. There isn’t “plain PHP code” and “Laravel code”. You are free to use native PHP functions in a Laravel application.

As @Robstar says, you can use name attributes with different values in your submit buttons, and then perform the intended action based on the value:

<form action="" method="POST">
    <!-- fields -->
    <button type="submit" name="action" value="save">Save</button>
    <button type="submit" name="action" value="preview">Preview</button>
    <button type="submit" name="action" value="advanced_edit">Advanced edit</button>
</form>
public function store(Request $request)
{
    switch ($request->input('action')) {
        case 'save':
            // Save model
            break;

        case 'preview':
            // Preview model
            break;

        case 'advanced_edit':
            // Redirect to advanced edit
            break;
    }
}
28 likes
deGecko's avatar

It's a little old to hard refresh the page to show some advanced field and then handle the form again; same thing for previewing something.

The main button should submit the form and the other two should do something in the same page, with the help of JavaScript.

  • The Preview button should open a modal, as an example, with the preview, or maybe just toggle off the form accordion-style and show the preview.
  • The Advanced button should just display the new field below the current form.

When they submit, you just check if the advanced field has been filed, if not, you ignore it. The validation rule would be something like this:

'advanced-field' => 'sometimes|min:10'

If you're expecting a message, minimum 10 characters long.

p0t4t0's avatar

@deGecko I know but I am keeping everything strictly PHP/Laravel with this project to help me understand Laravel more.

@martinbean Yes, I am aware about that part. I am just making sure whether or not there is an already existing Laravel function that can handle multiple submit buttons For example, $request->input('action') is I believe a Laravel function that is equivalent to $_POST['action'] in plain PHP.

(Sorry about the bad explanation. I find it hard sometimes to explain what I am thinking in English.)

But to get things back on track. The code I have above is already working. But I am just wondering what the difference is between $request->input('action') and $request['action'], because the latter is what I have in my code and it seems to be working just fine.

martinbean's avatar

@p0t4t0 Accessing $request as an array (i.e. $request['action']) or property ($request->action) will search multiple “bags” of request data. I just prefer to be a little more explicit, i.e.

  • If I want some POST data then I’ll use $request->input('key')
  • If I want a query string parameter I’ll use $request->query('key')
  • If I want a route parameter, I’ll use $request->route('key')

And so on.

It prevents little bugs where you might be intending to get data from say, the route, but instead end up with the value from a form field with the same name.

3 likes
dokohler's avatar

I had the same issue and solved it with Javascript.

 <input type="submit" value="Submit to alternative action" data-submit-action="/new-target">
 <input type="submit" value="Submit to alternative target" data-submit-target="_blank">
 <input type="submit" value="Submit as delete" data-submit-method="delete">
 <input type="submit" value="Submit with confirmation" data-submit-confirmation="Are you sure?">

Of course i needed to create a manipulator: https://gist.github.com/kohlerdominik/af78ecaec7a83e566e6fe8170f5f11bd

1 like
babonday's avatar

@MARTINBEAN - what does the route web.php file look like in this example?

Route::post('/search', 'SearchController@wtf');

thx

babonday's avatar

@ROBSTAR - i think he means the confusion when you first follow the laracasts.. it implies that laravel is an alternative to php syntax and also a shortcut.. . so you spend ages looking for the magic new way/ shortcut .

...which of course ironically wastes days in time...

Robstar's avatar

@BABONDAY - People really need to get out of the "Laravel" way mindset. Laravel is PHP. I'm a little unclear on why you think Laracasts is pushing something other than PHP. If you think like this I;d recommend you go and learn the basics of PHP so Laravel makes more sense.

There is no need to "spend ages looking for the new magic way" as Laravel has excellent documentation.

Please or to participate in this conversation.