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 }
Related
I've multiple forms with a single action, a single php page that gets called by all the forms.
How can I differentiate which form was sent to the php page?
Using a different unique input type="hidden" for each form.
HTML:
<input type="hidden" name="form_id" value="1">
<input type="hidden" name="form_id" value="2">
PHP:
$myform = $_POST["form_id"];
You can also use the submit button but note that the "value" parameter is what gets displayed to the user so you won't be able to modify it (assuming you want the same text to be displayed on every button).
<input type="submit" name="action" value="the user saw this">
PHP:
$_POST["action"] // -> "the user saw this";
Add a hidden field (action or the like) to each field, then check for it.
<form id="num1">
<input type="hidden" name="action" value="first_action" />
</form>
...and the check:
<?php
if(!empty($_REQUEST['action']) {
switch($_REQUEST['action']) {
case 'first_action':
// first action code
break;
}
}
?>
Give each submitbutton an other name or put a with different values in each form.
You can detect this from the submit button itself too, if submit has different values like:
update name, update profile, delete users...
On the submit button for each form, use different names. Something like:
<input type="submit" name="submit_1" value="Submit" />
<input type="submit" name="submit_2" value="Submit" />
<input type="submit" name="submit_3" value="Submit" />
...
Then on your PHP, you'll have:
$_POST["submit_1"]
$_POST["submit_2"]
$_POST["submit_3"]
Well, it's the first time I'm using stackoverflow, so I'd looking foward to be clear.
How can I set a value for a PHP variable from a button press?
For example, if the user pressed form button 1, $variable = 'false'; if it presses form button 2, $variable = 'true'... is that possible?
Thx, in advance
Yes, set different values to same name submit buttons
like:
<input name="submitBtn" type="submit" value="Choice1">
<input name="submitBtn" type="submit" value="Choice2">
Then depending on which button is pressed in php $_POST['submitBtn'] will have value Choice1 or Choice2
Yes, this is possible.
You can put 2 (or more) submit buttons with the same name and different values into a form and then chack the value of the submit button via php:
html:
<form method="POST" action="somefile.php">
<input type="submit" name="sb" value="button1" />
<input type="submit" name="sb" value="button2" />
</form>
php:
<?php
$submitbutton = $_POST['sb'];
?>
In the HTML side just create two submit button:
<input name="click" type="submit" value="clickOne">
<input name="click" type="submit" value="clickTwo">
Then on the PHP side (assuming you are submitting with POST method):
switch ($_POST['click']) {
case 'clickedOne':
// first pressed
break;
case 'clickedTwo':
// second pressed
break;
default:
// something wrong
break;
}
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.
Using a form with a blank action - action="".
I have 2 buttons on the form that do different things. one to submit/save the info, the other to open an output sheet:
<input type="submit" name="SubmitSave" id="SubmitSave" value="Submit / Save" onClick="this.form.action='PA_Monitorcall.php'; this.form.submit()" />
<input type="submit" name="EmailDetails" id="EmailDetails" value="Email" onClick="this.form.action='OutputSheetPA.php'; this.form.submit()" />
I need the output sheet to open in a new window, but can't have this in the form header details, it will need to go in the code for the button above. Any ideas?
Cheers!
onClick event of both submit buttons, call a javascript function, which would toggle the 'target' attribute of the form tag to '_blank' or '_parent'/''.
with this new value for 'target' attribute your post would be submitted in a new window/tab
<form target="" action="" method="post">
<input type="submit" value="Same Window" onClick="ChangeTarget('same')" />
<input type="submit" value="New Window" onClick="ChangeTarget('new')" />
</form>
function ChangeTarget(loc) {
if(loc=="new") {
document.getElementById('form_id').target="_blank";
} else {
document.getElementById('form_id').target="";
}
}
Use type="button" instead. Your onClick already calls submit, so you don't need them to be submit inputs.
You can name the two inputs with the same name and then check the value of that inputs.
<form action="">
<input type="submit" name="submit" id="SubmitSave" value="Submit / Save" />
<input type="submit" name="submit" id="EmailDetails" value="Email" /></form>
</form>
And in the php file:
<?php
if ($_POST['submit'] == 'Submit / Save')
// save the form input
elseif ($_POST['submit'] == 'Email')
// do other stuff
...
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.