In brief: is it possible to have multiple submit buttons on a page, and to detect on the receiving page which submit button was clicked?
For example, imagine a page that displays a random movie poster. There are two buttons: "I liked it", "I hated it." When either button is clicked, the form is submitted via POST to itself, where the user's like/dislike is noted in the database before displaying a new random poster.
My instinct was to write
<form action="thispage.php" method="post">
<input type="submit" name="like" value="I liked this." />
<input type="submit" name="dislike" value="I hated this." />
</form>
And then, on the receiving page,
if ($_POST['like'] == 1) ...
But that didn't work. Nor did
if ($_POST['submit'] == "like") ...
So I'm not sure what to do. Can anyone offer me a tip?
Option one is the typical beginners way. You check if your POST array exists, then you check the stored value if it does exist.
Checking if it exists, and checking the exact value prevents output on initial page load.
<?php
if(isset($_POST['action']) && $_POST['action'] == 'like') {
echo "You like it!";
} elseif (isset($_POST['action']) && $_POST['action'] == 'hate' {
echo "You hate it :(";
}
?>
<form action="thispage.php" method="post">
<input type="submit" name="action" value="like" />
<input type="submit" name="action" value="hate" />
</form>
OR....
The switch/case allows you to run a number of predetermined 'answers' against the value of the POST['action'] var.
You can also have a default value if none of the conditions are met.
Read more here: http://www.php.net/manual/en/control-structures.switch.php
<?php
switch($_POST['action']) {
case "like":
echo "You like it";
break;
case "hate":
echo "You hate it";
break;
}
?>
<form action="thispage.php" method="post">
<input type="submit" name="action" value="like" />
<input type="submit" name="action" value="hate" />
</form>
Yes, it is possible. You have the right idea. However, your checks are wrong in your if statements. Rather than checking for a specific value, since the submit buttons have different names you can simply check for their presence in the POST data.
if (isset($_POST['like'])) {
If you wanted to check for the specific value, you would use something like:
if (isset($_POST['like']) && $_POST['like'] === 'I liked this.') {
As you are just learning, I recommend getting familiar with debugging techniques. The easiest in this case to verify the data you get from the form would be to use print_r($_POST).
Try this
<form action="" method="post">
<input type="submit" name="like" value="I liked this." />
<input type="submit" name="dislike" value="I hated this." />
</form>
<?php
if(isset($_POST['like']) ){
echo "User says: ". $_POST['like'];
}
if(isset($_POST['dislike']) ){
echo "User says: ". $_POST['dislike'];
}
?>
Related
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
}
Okay this is kind of a noob question but I have been searching and searching and can not find out how to do this. I can not get the values of my radio buttons useing php.
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST">
Do you buy your lunch?
<br>
<input type="radio" name="doyoubuy" value="Yes" checked>Yes
<br>
<input type="radio" name="doyoubuy" value="No">No
<br><br>
<input type="submit" value="submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['doyoubuy'])) {
echo "You have selected :".$_POST['doyoubuy'];
}
}
?>
</body>
</html>
Thank you!!!
You are checking if (isset($_POST['submit'])) {
But your submit button is not actually named...
So just name it:
<input type="submit" value="submit" name="submit">
Then your code for the radios should work fine.
The action should be the current page. In your case is ""
I suggest this HTML and PHP for your consideration:
<html>
<body>
<form method="POST">
Do you buy your lunch?
<br>
<input type="radio" name="doyoubuy" value="Yes" checked>Yes
<br>
<input type="radio" name="doyoubuy" value="No">No
<br><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST) && $_POST != NULL) {
if ( $_POST['doyoubuy'] == 'Yes' || $_POST['doyoubuy'] == 'No') {
$answer = $_POST['doyoubuy'];
echo "You selected :". $answer;
}
}
?>
</body>
</html>
What needs to be tested is whether the form was submitted. If so, provided that each field bears a name in the form, then a value of some kind will be associated with every field, which obviates the need to test if the submit button is set. Also, if a user submitted a form but neglected to fill in, for example, a text input, then that field's value would still be set; it would be set to an empty string.
It is inadvisable to trust a posted form value and consider it safe to use. In this case, I test to be sure that the answer is what I'm expecting, just in case someone might have spoofed the form and changed the answer to something other than what I intended.
By default, if one leaves out the ACTION attribute of the form, its value is the url of the page containing the form.
I have one page with a simple post form
<form name="click" action="UserOrders.php" method="post">
<input type="hidden" name="amount" value="<?php echo $total ?>">
<input type="image" src="/Templates/images/UpdateButton.png" name="submit">
</form>
and the only thing I want to check is if it submits.. In the other page "UserOrders.php" I just wrote
<?php
if ($_POST['submit']){
echo $_SESSION['ID'];
}
It seems to me irregular that it doesn't work and I would like another set of eyes to check it out.
(if i put the echo $_SESSION['ID'] outside the brackets, it works.)
An input type="image" only defines that image as the submit button and not as an input that can carry over a value to the server. source
An alternative way to see if the form was submitted is to check $_SERVER['REQUEST_METHOD']
if ('POST' === $_SERVER['REQUEST_METHOD']) {
// submitted
}
I agree with the previous answer. You are not actually putting any information to the form and therefore there is nothing for the new form to react to. $_POST['submit"] is looking for data that was sent and not for the method of sending.
There's no actual submit button. You need a
<form action="UsersOrders.php" method="POST">
<input type="whatever you want" name="input">
<input type="submit" name="submit">
</form>
Now if you go into UsersOrders.php and do what you did before:
if($_POST['submit']) {
echo $_POST['input'];
}
You will actually get some input.
I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}
I'm encountering a problem. I'm using Wordpress, but this ain't a Wordpress question.
I'm using two forms, on one form I have all the input fields and one hidden input field which I use for checking which form the user has submitted. I have saved its value as 'save'. There is another form which is just for resetting all the options and its value is 'reset'. In PHP, I check the value of the hidden field and take respective actions. But the problem is that the reset thingy isn't working.
Here is my HTML for the forms:
<fieldset>
<form method="post">
<!-- Some input fields here-->
<p class="submit">
<input name="save" type="submit" value="Save changes" />
<input type="hidden" name="action" value="save" />
</p>
</form>
</fieldset>
<fieldset>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</fieldset>
In PHP, I verify them like this:
// if I change the 'save' literal to something else like 'savea', $_POST variable will not be empty
// but if I dont, then $_POST variable is NULL
if ('save' == $_POST['action']) {
foreach ($this->cp_options as $option) {
if (isset($_POST[$option['id']])) {
update_option($option['id'], $_POST[$option['id']]);
}
else {
delete_option($option['id']);
}
}
header("Location: themes.php?page=functions.php&saved=true");
die;
}
// if I change the 'reset' literal to something else like 'reseta', $_POST variable will not be empty
// but if I dont, then $_POST variable is NULL
elseif ('reset' == $_POST['action']) {
foreach($this->cp_options as $option) {
delete_option($option);
}
header("Location: themes.php?page=functions.php&reset=true");
die;
}
The problem is if I change the 'reset' or 'save' literal to anything else like 'reseta' or 'saveasdfasd', $_POST variable won't be empty, but if I dont, then $_POST variable is NULL.
Any ideas on why this is happening?
[Old Answer Redacted]
EDIT
Try to isolate your testing environment first. This gave me results I expected.
<?php
if ( isset( $_POST['action'] ) )
{
switch( $_POST['action'] )
{
case 'save':
echo 'Save Action Requested';
break;
case 'reset':
echo 'Reset Action Requested';
break;
default:
echo 'Unknown action requested:';
var_dump( $_POST['action'] );
}
} else {
echo 'No action parameter received';
}
?>
<fieldset>
<form method="post">
<!-- Some input fields here-->
<p class="submit">
<input name="save" type="submit" value="Save changes" />
<input type="hidden" name="action" value="save" />
</p>
</form>
</fieldset>
<fieldset>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</fieldset>
I know you said that $_POST is null but are you assuming that or did you actually check $_POST == null? Have you tried doing a var_dump($_POST) to print out exactly what is getting sent across? Just comment out your re-direction and see what is in $_POST. Maybe that will give you a better clue as to what is happening.
Simple... remove the hidden inputs and change both of your "submit" buttons to have the same name, but different values:
<input type="submit" name="action" value="Reset" />
<input type="submit" name="action" value="Save" />
Then you can test it like this:
if ($_POST['action'] === 'Reset') {
// Do a reset here
} else {
// Do a save here
}
And you probably want to wrap the whole thing in:
if (isset($_POST['action'])) {
// Put your form handling here
}
If you have multiple forms on one page I'd recommend you send each form to a different URL. This is by far the simplest and most reliable way to detect where the form is going, just have two different scripts to deal with processing that form. You can then include or redirect to the final page you want the user to see.
This might because there are duplicate elements with the same name.
Can you try putting an id or name to your form?