Michael Tot Korsgaard
Michael Tot Korsgaard

Reputation: 4014

solution for nested forms

is there a way to have a nested form?

like this

<form action="" method="post">
    <input type="text".... />
    <input type="text".... />
    <form action="some action">
        <input type="submit" .... />
    </form>
</form>

if not, how would I go around getting the same function without nested forms

Upvotes: 3

Views: 4794

Answers (1)

Nikos Grigoriadis
Nikos Grigoriadis

Reputation: 2912

No you can't have nested forms but there is a simple solution to your problem.

<form action="" method="post">
    <input type="submit" name="action" value="action1" />
    <input type="submit" name="action" value="action2" />
</form>

If you use PHP this code will handle your form submissions:

//if user clicks on the first submit button, action1 is triggered
if (isset($_POST['action']) && $_POST['action'] == 'action1') {
    // do something    
}

//if user clicks on the second submit button, action2 is triggered    
if (isset($_POST['action']) && $_POST['action'] == 'action2') {
    // do something else
}

ps: As I see you work with C# and .NET, this would be translated into:

public class ExampleController : Controller
{
    ....

    [HttpPost]
    public ActionResult Index(string action)
    {

        if (!string.IsNullOrEmpty(action) == "action1") {
            // Do something
        }

        if (!string.IsNullOrEmpty(action) == "action2") {
            // Do something
        }
    }

    ...
}

Upvotes: 4

Related Questions