3

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 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.

asked Jan 18, 2010 at 12:49

4 Answers 4

10

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.
answered Jan 18, 2010 at 12:54
Sign up to request clarification or add additional context in comments.

Comments

3

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.

answered Jan 18, 2010 at 12:55

Comments

0

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

answered Jan 18, 2010 at 12:53

Comments

0

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.

answered Jan 18, 2010 at 13:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.