I have a form with multiple submit buttons.
Each submit button is an IMG SRC trash can which denotes the delete icon for messages in a web based messaging mail inbox
what is the best way to figure out which submit button icon was clicked so that I can then write the PHP/MySQL code to DELETE the message?
if(!empty($_POST)){
// How do I figure out which submit button has been clicked to get the ID of the message to delete?
}
<form method="POST">
<input src="http://www.foo.com/img.png" id="button_1">
<input src="http://www.foo.com/img.png" id="button_2">
<input src="http://www.foo.com/img.png" id="button_3">
<input src="http://www.foo.com/img.png" id="button_4">
...
<input src="http://www.foo.com/img.png" id="button_100">
</form>
Set value for each submit button and check that in php and find which one is clicked
<form method="POST">
<img src="http://www.foo.com/img.png" id="button_1" name="submit_btn" value="1">
<img src="http://www.foo.com/img.png" id="button_2" name="submit_btn" value="2">
<img src="http://www.foo.com/img.png" id="button_3" name="submit_btn" value="3">
<img src="http://www.foo.com/img.png" id="button_4" name="submit_btn" value="4">
...
<img src="http://www.foo.com/img.png" id="button_100" name="submit_btn" value="100">
</form>
echo $_POST['submit_btn']; will give you the value of which submit button is clicked
Give each button a name=""
Then you can do something like
isset($_POST['button_name']) {
// execute code here if true
}
THE solution of this problem is to use the NAME attribute of the tag input/button.
<input type="submit" name="submitSave" value="Save"/>
<input type="submit" name="submitAddComment" value="Add comment"/>
or
<button type="submit" name="submitSave">Save</button>
<button type="submit" name="submitAddComment">Add comment</button>
I think you can also use the value attribute of button tag, this is definitively not possible with input tag.
If you need to use an ID or another variable, use name="submitDelete[888]"
Then, check it with PHP:
if( isset($_POST['submitDelete']) ) {
echo key($_POST['submitDelete']);// Displays the ID to delete, e.g. 888.
}
So many years later, I like button because it allows to display a text or an image independently of the value returned.
Here is an illustration of possibilities which fits the title of this post and more cases than the OP.
<?php
if(!empty($_POST['id'])){
echo 'button '. $_POST['id'] .' clicked';
} elseif ('create' === ($_POST['action'] ?? '')) {
echo 'create clicked'; // ?action=create
} elseif (isset($_POST['action'])) {
echo 'refresh clicked'; // ?action
} elseif (isset($_POST)) {
echo 'Default clicked'; // ?
}
?>
<form method="POST">
<!-- Original Post examples -->
<button type="submit" name="id" value="1"><img src="http://www.foo.com/img.png"></button>
<button type="submit" name="id" value="2"><img src="http://www.foo.com/img.png"></button>
...
<button type="submit" name="id" value="100"><img src="http://www.foo.com/img.png"></button>
<!-- Additional possibilities -->
<!-- ?action=create -->
<button type="submit" name="action" value="create">New element</button>
<!-- ?action -->
<button type="submit" name="action">Refresh</button>
<!-- ? -->
<button type="submit">Default</button>
</form>
you can give a name and a value to each of your buttons. It will then show up under $_POST['submit']
<img src="http://www.foo.com/img.png" id="button_4" name='submit' value='4' />
You have to pass your value to the current file by declearing name and value for each.. then you can echo in your php script in order to know which one is clicked.
Related
I was wondering if it was in any way possible to be able to determine the ID of the form that was posted on PHP?
<form id="ES1A" action="enroll.php" method="post">
<input type="checkbox"/>
<button type="submit" class="btn btn-primary">Next Step</button>
</form>
In the enroll.php
if(isset($_POST['ES1A']))
{
//Code after checking that the form that was submitted indeed has the ID of ES1A
}
P.S: I'm not too sure on how i would do this on PHP. Thank you in advance
Post does not use the ID of the element, rather the name, so instead of your current form, you could use;
<form name="ES1A" action="enroll.php" method="post">
<input type="checkbox"/>
<input name="formid" value="ES1A" /><!-- This holds your form id so you can use this -->
<button type="submit" class="btn btn-primary">Next Step</button>
</form>
And in the PHP;
if (isset($_POST['ES1A']) // Unsure if form itself will be posted with the submit
{
// This is set as it uses the name of the element
$formid = $_POST['formid']; // Get the ID of the form from the element passed in
}
If form's name attribute does not work you can always set name for button:
<button name="ES1A" type="submit" class="btn btn-primary">Next Step</button>
Or:
<input name="ES1A" type="submit" class="btn btn-primary" value="Next Step">
PHP part should be the same as you already have:
if(isset($_POST['ES1A']))
{
//Code after checking that the form that was submitted indeed has the ID of ES1A
}
an alternative is to use hidden input
<input name="ES1A" value="formid" type="hidden" />
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
}
I need the button text displayed to be different from the sent form value.
Is there a way to achieve this using CSS|HTML|PHP? Here's my pseudo.
<form method='post' action='index.php'>
<input type="submit" value="name1" name="remove">Remove 1</input>
<input type="submit" value="name2" name="remove">Remove 2</input>
<input type="submit" value="name3" name="remove">Remove 3</input>
</form>
<?php
#Pressing Remove 1 will print "name1", Remove 2 will print "name2", etc.
if(isset($_POST['remove']))
{
$_gone = $_POST['remove'];
print $_gone
}
?>
Use a button element (which can have child nodes), not an input element (which cannot).
<button type="submit" value="name1" name="remove">Remove 1</button>
Note that old IE has limitations here.
This form should calculate numbers and save
Now there are two buttons One is call Calculator and two call Save
If I press Calculator
I get the form action is going to file name save.php And I do not want it that way
How can I set it up that button do something else
Example
Calculator = Calculator
Save = save.php
Is it possible to set it
Because it is one form
Thanks to anyone who can help
<?php
error_reporting (0);
$NUM = $_POST["NUM"];
$NUM2 = $_POST["NUM2"];
$NUM = "$NUM";
$NUM2 = "$NUM2";
$subtotal= $NUM+$NUM2;
?>
<form action="save.php" method="POST" name="Calculator">
<p>
<input name="NUM" type="text" value="<?php echo $_POST["NUM"]; ?>" />
</p>
<p>+</p>
<p>
<input name="NUM2" type="text" value="<?php echo $_POST["NUM2"]; ?>" />
</p>
<p>
<input name="subtotal" type="text" value="<?php echo "$subtotal";?>" />
</p>
<p>
<input name="submit" type="submit" value="Calculator" />
<p>
<input name="submit" type="submit" value="Save" />
</p>
</form>
You can have all the logic in a single PHP script (no need to direct to a different script depending on the button). If the logic is complicated, use include statements in order to separate the code.
Name the buttons differently:
<input name="calculator_submit" type="submit" value="Calculator" />
<input name="save_submit" type="submit" value="Save" />
Then in PHP:
if (isset($_GET['calculator_submit'])) {
// ...
} else if (isset($_GET['save_submit'])) {
// ...
} else {
// ...
}
If you really need different PHP script, then you'll have to go with Javascript (function will change the form action when a submit is clicked).
Since you are now using two submit buttons, both will submit the form and go to save.php.
Make your "calculator" button an input type=button instead of submit, and handle it via JavaScript.
Just FYI:
HTML5 allows to define a different form target URL by specifying the formaction attribut on a submit button – but browser support is lousy as of now.
Form and Buttons
<input name="submit" type="button" onclick="submitForm('Calculator')" value="Calculator" />
<input name="submit" type="button" onclick="submitForm('Save.php')" value="Save" />
Some jquery:
function submitForm(path) {
$('#Calculator').attr('action', path);
$('#Calculator').submit();
}
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
...