开发者

PHP - HTML form two possible actions?

I'm creating a registration page and I would like to give the user the opportunity to review 开发者_StackOverflow中文版their information and go back and edit it before clicking a confirm button which inserts it into the database.

Is there a way to include two submit buttons which point to different scripts or would I have to duplicate the entire form but use hidden fields instead?

Any advice appreciated.

Thanks.


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 what submit button has been activated:

if (isset($_POST['next'])) {
    // next step
} else if (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.


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">
    <input type="text" name="input">
    <input type="submit" name="action" value="Add">
    <input type="submit" name="action" value="Edit">
</form>

with

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

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


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>
<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");
}
?>

or via javascript


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜