Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Form with Multiple Submit Buttons
  • You might have come across a situation where you want to have a form with multiple submit buttons and have a different controller action for each button.

    Unfortunately, static html forms only allow one action per form.

    This is how I solved this using Javascript:

    View (forma.php):
    <form id="forma">

        <table border = "1">
        <tr><td>User:</td><td><input type="text" name="user"></td></tr>
        <tr><td>Password:</td><td><input type="text" name="password"></td></tr> 
        <tr>
        <td><input type="submit" id="register" name="action" value="register"></td>
        <td><input type="submit" id="logon" name="action" value="logon"></td>
        </tr>

    </table>
    </form>
     View Template (template.php):
     <!DOCTYPE html>
    <html>

        <head>

            <meta charset="utf-8">
            <title><?php echo $title ?></title>
            <?php echo Asset::js($script); ?>

        </head>
        <body>

            <?php echo $form; ?>

        </body>

    </html>
    Javascript (forma.js):

    window.onload = init;
    function init()
    {

        var logonbutton = document.getElementById("logon");
        logonbutton.onclick = HandleLogonButton;
        var registerbutton = document.getElementById("register");
        registerbutton.onclick = HandleRegisterButton;

    }
    function HandleLogonButton(e)
    {

        var form = document.getElementById("forma");
        form.action = "logon";
        form.method = "post";
        form.submit();

    }
    function HandleRegisterButton(e)
    {

        var form = document.getElementById("forma");
        form.action = "register";
        form.method = "post";
        form.submit();

    }

     

    Controller (logon.php):
    public function action_index
    {

        $this->template->form = View::forge(''forma.php");
        $this->template->title = "Form A";
        $this->template->script = "forma.js";

    }
    public function action_register()
    {
    // whatever you want
    }
    public function action_logon()
    {
    // whatever you want
    }
  • This would have been a lot shorter:
    <input type="submit" id="logon" name="action" value="logon" onclick="forma.action='/controller/action/parameters';">

  • Yes, that also works. Thx.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion