Skip to content Skip to sidebar Skip to footer

Php - Html Form Two Possible Actions?

I'm creating a registration page and I would like to give the user the opportunity to review their information and go back and edit it before clicking a confirm button which insert

Solution 1:

You can use two submit buttons with different names:

<inputtype="submit" name="next" value="Next Step">
<inputtype="submit" name="prev" value="Previous Step">

And then check what submit button has been activated:

if (isset($_POST['next'])) {
    // next step
} elseif (isset($_POST['prev'])) {
    // previous step
}

This works because only the activated submit button is successful:

  • If a form contains more than one submit button, only the activated submit button is successful.

Solution 2:

As for every other HTML input element you can just give them a name and value pair so that it appears in the $_GET or $_POST. This way you can just do a conditional check depending on the button pressed. E.g.

<form action="foo.php" method="post">
    <inputtype="text" name="input">
    <inputtype="submit" name="action" value="Add">
    <inputtype="submit" name="action" value="Edit">
</form>

with

$action = isset($_POST['action']) ? $_POST['action'] : null;
if ($action == 'Add') {
    // Add button was pressed.
} elseif ($action == 'Edit') {
    // Edit button was pressed.
}

You can even abstract this more away by having actions in an array.

Solution 3:

you can

like :

http://sureshk37.wordpress.com/2007/12/07/how-to-use-two-submit-button-in-one-html-form/

via php

<!--    userpolicy.html      --><html><body><formaction="process.php"method="POST">
    Name <inputtype="text"name="username">
    Password <inputtype="password"name="password"><!--  User policy goes here           --><!--            two submit button            --><inputtype="submit"name="agree"value="Agree"><inputtype="submit"name="disagree"value="Disagree"></form></body></html>

/*         Process.php         */
<?phpif($_POST['agree'] == 'Agree') {
    $username = $_POST['username'];
    $password = $_POST['password'];
      /* Database connection goes  here */

}
else {
    header("Location:http://user/home.html");
}
?>

or via javascript

Solution 4:

I certainly wouldn't repeat the form, that would be a fairly self-evident DRY violation. Presumably you will need the same data checks every time the form is submitted, so you could perhaps just have the one action and only run through the "add to database" part when the user hits the "approve" button.

Post a Comment for "Php - Html Form Two Possible Actions?"