HTML - form of two possible actions?

I am creating a registration page, and I would like to give the user the opportunity to view their information and return and edit it before pressing the confirmation button, which inserts it into the database.

Is there a way to enable two submit buttons that point to different scenarios, or will I have to duplicate the entire form, but use hidden fields instead?

Any advice is appreciated.

Thank.

+3
source share
4 answers

You can use two submit buttons with different names:

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

And then check which submit button has been activated:

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

, :

  • , .
+10

HTML input, , $_GET $_POST. , . .

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

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

, .

+3

like:

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

php

<!--    userpolicy.html      -->
<html>
<body>
<form action="process.php" method="POST">
    Name <input type="text" name="username">
    Password <input type="password" name="password">
    <!--  User policy goes here           -->

    <!--            two submit button            -->
    <input type="submit" name="agree" value="Agree">
    <input type="submit" name="disagree" value="Disagree">
</form>
</body>
</html>

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

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

javascript

0

Of course, I would not repeat the form, this would be a rather obvious violation of DRY. Presumably, you will need the same data validation every time the form is submitted, so you could just have one action and only perform the “add to database” part when the user clicks the approve button.

0
source

Source: https://habr.com/ru/post/1728914/


All Articles