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.
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.
Related
I have HTML form with three Submit Buttons.
One is for reloading the page, the other one makes a database entry and the last one is a simple login button. Each button is also in a div.
When i try to login, i normally enter my username & password and then press enter (Im not tabbing out of the textbox). But instead of pressing the button "login", it presses the button "reload".
I found some ways with Javascript, JQuery and AJAX, but im not allowed to use any of these. All i use is PHP, CSS and HTML.
Is there a way to choose which Button i want to press when i use enter?
edit:
This is the form with the three submit buttons:
<form method="post"action="Sub.php">
<input type="text" id="txt" name="txt" value="text">
<input type="submit" id="refresh" name="refresh" value="Refresh">
<input type="submit" id="update" name="update" value="Update">
<input type="submit" id="login" name="login" value="Login">
<input type="text" id="txt" name="txt" value="text"></form>
This is the file, where i post it.
<?php
if(isset($_POST['login']))
{echo "Login has been pressed";}
if(isset($_POST['update']))
{echo "Update has been pressed";}
if(isset($_POST['refresh']))
{echo "Refresh has been pressed";} ?>
If i tab through the first file and press enter in one of the textboxes, i will allways press the submit button with the value "refresh".
Convert your Submit type button to simple button type
$("#textbox_password_id").keyup(function(event){
if(event.keyCode == 13) {
$("#form_submit").submit();
}
});
Other way you can place your Login button first, so whenever you will press it will take first submit click
Duplicate of How do I use two submit buttons, and differentiate between which one was used to submit the form?
Give each input a name attribute. Only the clicked input's name attribute will be sent to the server.
<input type="submit" name="reload" value="Reload"/>
<input type="submit" name="database" value="Database Entry"/>
<input type="submit" name="login" value="Login"/>
<?php
if (isset($_POST['reload'])) {
// Reload-button was clicked
}
elseif (isset($_POST['database'])) {
// Database-button was clicked
}
elseif (isset($_POST['login'])) {
// Login-button was clicked
}
else {
// Failure
}
?>
I have two forms, one with login and another with logout and they both use the same controller/form processor,
I am using
$_SERVER['REQUEST_METHOD']
to see if the form is submitted.
But how can I know if login or logout was clicked.
$_POST['action'] == 'login'
and
$_POST['action'] == 'logout'
are not working.
okay here is the complete form :
<?php
$test = 'default';
if( $_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['action'] == 'login' ){
//do some stuff
$test = 'login';
}elseif( $_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['action'] == 'logout' ){
//do other stuff
$test = 'logout';
}
?><?php echo $test; ?>
<form method="post" action="">
<p>
<input type="text" name="username" placeholder="Username" value="">
</p>
<p>
<input type="password" name="password" value="" placeholder="Password">
</p>
<p>
<input type="submit" name="login" value="Log In">
</p>
</form>
<form method="post" action="">
<input type="submit" name="logout" value="Log Out">
</form>
But the $test doesn't change.
Give the buttons different names
If you have a input element of type submit, you will get a button that posts a value itself. Give it a name and a value, and that value will be posted along with the post data:
<input type="submit" name="button1" value="Click me">
<input type="submit" name="button2" value="Or me">
The value is often localized and I think you wouldn't want to check for that, but you can just check for the existence of Button1 or Button2 in the post variables to see which one was clicked, regardless what their value is.
Give the buttons (only) different values
Alternatively, if you know the button value is useful (it contains an id or name rather than a localized text from a template), you could give both buttons the same name (like 'action') and check the value of the post variable instead. In that case, the two buttons behave more or less like a group of radio buttons. Not my preference, but certainly possible and acceptable.
<input type="submit" name="action" value="Action 1">
<input type="submit" name="action" value="Action 1">
Or you could use a button tag in that case, which is a similar but slightly different element. It also has a name and value attribute, but you can specify the text as the content of the element, so you can decouple the value from the text you present to the user. This would be better than using the input version above.
<button type="submit" name="action" value="Action 1">Click me for action 1!</button>
<button type="submit" name="action" value="Action 2">Secondary action</button>
Note that type="submit" is the default for buttons, so you can omit it, as long as you don't need to support IE7.
Checking which one was clicked
Whichever solution you pick, don't forget to check thoroughly though. There are other ways of submitting a form, for instance through clicking enter, so make sure to propertly handle the case where neither button was clicked.
if (array_key_exists('button1', $_POST)) {
// Button 1 was clicked
} elseif (array_key_exists('button2', $_POST)) {
// Button 2 was clicked
} else {
// Neither was clicked.
}
or for the alternative
if (array_key_exists('action', $_POST)) {
switch ($_POST['action') {
case 'Action 1':
// Button 1 was clicked
break;
case 'Action 2':
// Button 2 was clicked
break;
default:
// An unknown button was clicked!
break;
}
} else {
// Neither was clicked.
}
Assign names to the buttons:
<input type="submit" name="Add" value="Add" />
<input type="submit" name="Delete" value="Delete" />
and verify which one is set as:
if (isset($_POST['Add'])) {
// add
} elseif (isset($_POST['Delete'])) {
// delete
}
You can identify through isset function:
if(isset($_POST['action']) && $_POST['action']=="login"){
//Login Button Logic
} else if(isset($_POST['action']) && $_POST['action']=="logout"){
//Logout Button Logic
}
Say I have a simple form on a page called photo/delete.php. This form deletes an image specified by the user. All it is, is this:
<form action="?" method="post">
<input type="checkbox" name="confirmDelete" value="confirm" />
<input type="submit" value="delete" />
</form>
So, this form contains a confirmation check box that must be ticked to ensure the image is deleted. How can I dynamically choose what page to POST this form to, based on its contents?
For example, if the checkbox is not checked, yet the submit button is clicked, I'd like to stay on the same photo/delete.php page and display an error, since its possible they really do want to delete the image and simply forgot to tick the box.
But otherwise, if everything is successful and the checkbox is ticked, I'd like to POST it to another page, say home.php since it makes no sense to stay on the same page of a just-deleted image.
How can I implement this?
You may try something like this
HTML:
<form action="delete.php" method="post">
<input type="checkbox" name="confirmDelete" value="confirm" />
<input id="btn_delete" type="submit" value="delete" />
</form>
JS:
window.onload = function(){
document.getElementById('btn_delete').onclick = function(e){
var checkBox = document.getElementsByName('confirmDelete')[0];
if(!checkBox.checked) {
if(e && e.preventDefault) {
e.preventDefault();
}
else if(window.event && window.event.returnValue) {
window.eventReturnValue = false;
}
alert('Please check the checkbox!');
}
};
};
DEMO.
Is there a way to specify the header location based on which link the user has clicked? If possible, I'd prefer doing this in PHP.
I'd like to submit forms when the user clicks on any of a few buttons (i.e. Next, Back, Save, etc.), and they each need to redirect the user differently once the form is submitted.
Example
HTML - form2.html
<form name="form2" id="form2" method="post" action="form2-exec.php">
<!-- Form Elements -->
<input type="submit" value="BACK" />
<input type="submit" value="SUBMIT" />
</form>
PHP - form2-exec.php
// Connect to database
// Insert & ON DUPLICATE KEY UPDATE Statements
header("location: form3.php");
You do not need the anchor tags. Can you try something like:
<input type="submit" name="whereto" value="BACK" />
<input type="submit" name="whereto" value="SUBMIT" />
PHP - form2-exec.php
// Connect to database
// Insert & ON DUPLICATE KEY UPDATE Statements
if ($_GET['whereto'] == 'BACK') {
header("location: form1.php");
} elseif ($_GET['whereto'] == 'SUBMIT') {
header("location: form3.php");
}
You can find out more about PHP predefined variables at http://php.net/manual/en/reserved.variables.php
you can simply use $_POST['submit'] which will contain the value as a value. Use the name attribute on your submit buttons as well to make it sure.
I have several sections of code that insert values into different tables in my DB.
I am wondering if there is a way to capture which button on my form has been selected so when the page reloads it only executes the one insert statement?
this is how I submit to the same page
<form name="input" action=myawesomeform.php" method="POST">
This is what my submit button looks like
<input type="submit" value="Submit" id="5050gdmyButton" />
so pretty much I want to execute one of the insert statements depending on which button was selected..
any help would be greatly appreciated.
1) You need to give name attribute to your submit button
2) when you have multiple inputs in your form and when you press one of them, then you only get that one in your php code with name attribute.
So for example:
<input type="submit" name="submit1" value="add" id="5050gdmyButton1" />
<input type="submit" name="submit2" value="update" id="5050gdmyButton2" />
in your php
if(isset($_POST['submit1']) && $_POST['submit1'] == 'add'){
// do insert for first one
}
if(isset($_POST['submit2']) && $_POST['submit2']== 'update'){
// do update for second one
}
you can give two different names for submit button like this..
<input type="submit" name="button1" value="Button1" id="5050gdmyButton" />
<input type="submit" name="button2" value="Button2" id="5051gdmyButton" />
In the Receiving page use the condition like this...
if ($_POST['button1'])
{
//do your first button process
}
elseif($_POST['button2'])
{
//do you second button process
}
If you are having more than one submit button than you should make a different name for each button
<input type="submit" value="addSubmitButton" id="5050gdmyButton1" />
//to add value in db
<input type="submit" value="removeSubmitButton" id="5050gdmyButton2" />
//to remove value in db
in your serverside
<?php
if(isset($_POST["addSubmitButton"])){
//add value in db
}
if(isset($_POST["removeSubmitButton"])){
//remove data from database
}
?>
just but a name tag inside input
<input type="submit" value="Submit" id="5050gdmyButton" name="5050gdmyButton" />
and in php check if name isset()
if(isset($_REQUEST['5050gdmyButton'])){ //do somthing }