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?
Related
I am trying to change page based on user input in form. The user enters their tag and it changes to that users page with details. Currently, it just keeps attempting to change page then eventually times out. Here is my code:
SWITCH STATEMENT
if(isset($_POST['submit'])) {
$name = $_GET['clan_tag'];
switch($name) {
case "player1":
header("Location: commander.php");
break;
case "player2":
header("Location: officer.php");
break;
...//
default:
header("Location: index.php");
}
}
FORM
<form action="" method="get">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
So if user enters player 1 in form then submits, it should change to commander.php page, but it isn't.
Could anyone point me in correct direction thanks.
First of all, change first line to
if(isset($_GET['submit'])){
2nd give a 'name' attribute to submit button
<input type="submit" class="submit" value="SUBMIT" name="submit"/>
I just looked at your code and realized your form was sending data using the HTTP GET method but your PHP script was checking for HTTP POST method in the if(isset($_POST['submit'])). So I modified the PHP as shown below.
if(isset($_GET['clan_tag'])){
$name = $_GET['clan_tag'];
switch($name) {
case "player1":
header("Location: commander.php");
break;
case "player2":
header("Location: officer.php");
break;
default:
header("Location: index.php");
}
}
?>
I also modified the HTML as shown below.
<form action="" method="get">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
Try in this way:
In the FORM:
attribute method write "post" in action= write "path where is your php file"
i.e.
<form action="path/yourfile.php" method="post">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
in PHP file
if(isset($_POST['clan_tag'])) {
$name = $_POST['clan_tag'];
}
else{
$name="";//your code
}
....
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
}
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'];
}
?>
So I have this code on my page:
<form method="get" action="client_specific_task.php">
<input type="hidden" value="x" />
<input type="submit" value="Add Client-Specific Task">
</form>
client_specific_task.php has the following:
IF (!$_GET) {
ECHO '<html><head><title>Compliance</title></head><body><h1>Error - return home</h1></body></html>';
die();
}
I am continuously getting the Error - return home message.
I've done this a million times on other pages, not sure why it isn't working this time - am I missing something obvious?
Thank you everyone for your help and suggestions!
You have no successful form controls, so $_GET will be empty.
A control has to have a value and a name to be successful.
You need to have name attribute in form input fields:
<form method="get" action="client_specific_task.php">
<input type="hidden" name="name1" value="x" />
<input type="submit" name="name2" value="Add Client-Specific Task">
</form>
try this:
<form method="get" action="client_specific_task.php">
<input type="hidden" name="somename" value="x" />
<input type="submit" value="Add Client-Specific Task">
</form>
You have to have a name attribute.
Use $_SERVER['REQUEST_METHOD'] to determine if the user is getting.
Example:
if ($_SERVER['REQUEST_METHOD']=="GET") {
ECHO '<html><head><title>Compliance</title></head><body><h1>Error - return home</h1></body></html>';
die();
}
You said it yourself, check that it's empty and provide name to the fields
if (empty($_GET)) {
// $_GET is empty
}
This method is secure. !$_GET could give you an undefined variable E_NOTICE if $_GET was unset.
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();
}