isset() function not working properly for me - php

So I have PHP code like this
if (isset($_POST['btnChange'])) {
PHP CODE HERE
} else if (isset($_POST['btnUpdate'])) {
PHP CODE HERE
} else {
PHP CODE HERE FOR DELETE
}
My HTML code loks like this
<form action="decision.php" method="POST">
SOME HIDDEN INPUTS AND ON TABLE
<input type="submit" name="btnChange" value="Change">
<input type="submit" name="btnUpdate" value="Update">
<input type="submit" name="btnDelete" value="Delete">
</form>
So the problem is that when I click each button I straight go for DELETE section (else), my code is not checking for if-s.
I checked my code and it is correct only problem is probably in if(isset).

The displayed code look OK. Surely something you haven't shown us.
Just try your lines in a simple test file to be sure.
<?php
if (!empty($_POST)) {
if (isset($_POST['btnChange'])) {
echo 'btnChange';
} else if (isset($_POST['btnUpdate'])) {
echo 'btnUpdate';
} else {
echo 'else';
}
}
?>
<form method="POST">
<input type="submit" name="btnChange" value="Change">
<input type="submit" name="btnUpdate" value="Update">
<input type="submit" name="btnDelete" value="Delete">
</form>

Belive it or not,
my form looked like this
<form action="decision.php" action="POST">
Instead of
<form action="decision.php" method="POST">
Thank you all for taking time for this and sorry for wating you time, I was to tired.

Related

Return a php value from a form without ajax

It might be a beginner question but I'm really struggling with this. I want to put a html button in one of my page that will trigger a function.
But in order to do so I also need a php value from my page to be return from the button to the function.
I don't want to use Ajax for this.
//first file
if(isset($_POST['button']))
{
theFunction($i);
}
require_once 'file.php';
<?php
//second file
$i = 4; ?>
<form method="POST">
<input type="submit" name="button" value="buttonvalue">
</form>
you can send the value via a hidden form field.
...
//first file
if(isset($_POST['button'])){
$i=$_POST['i']; //<---
theFunction($i);
}
require_once 'file.php';
//second file
$i=4;
?>
<form method="POST">
<input type="hidden" name="i" value="<?php echo $i; ?>">
<input type="submit" name="button" value="buttonvalue">
</form>
//first file
if(isset($_POST['button']))
{
if(isset($_POST['INPUT_NAME']) && !empty($_POST['INPUT_NAME']))
{
theFunction($_POST['INPUT_NAME']);
}
}
require_once 'file.php';
<?php
//second file
<form method="POST">
<input type="hidden" name="INPUT_NAME" value="YOUR_VALUE_eg-4" requiered/>
<input type="submit" name="button" value="buttonvalue">
</form>
This method is not secured.
You can only use a simple html link like
TEXT ou if you make a good router you can use something like TEXT

switch function to change page based on user input not working

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
}
....

PHP - if(isset($_POST['button1'')){... if(isset($_POST['button2'])).. not working?

I have this code:
echo'<form method="POST" action="">';
echo'Code:<input type="text" name="name">';
echo'<input type="submit" name="save" value="Save">';
echo'</form>';
if(isset($_POST['save'])){
//something
echo'<form method="POST" action="">';
echo'Code:<input type="text" name="name">';
echo'<input type="submit" name="delh" value="Delete">';
echo'</form>';
if(isset($_POST['delh'])){
// Cant show this! :(
echo "Deleted!";
}
}
When i press "DELETE", the page reloads and the message "Deleted!" remains hidden.
This is a schedule. The idea is if someone presses the Save button but has already saved an hour it says "You have already saved an hour, do you want to cancel it?". When it clicks "Delete", the hour is deleted from the database.
In the case where the Save button is pressed, but the person has not saved an hour, the delete button is not displayed.
This line:
echo "Deleted!";
Can only be reached if both of these conditions are true:
if(isset($_POST['save'])){
//...
if(isset($_POST['delh'])){
But the form you're showing contains no element named save. The first condition is false, so the code inside that if block never runs. (It may have been true in a previous request, but not in the request you're making with this form.)
You may have meant to separate those conditions?:
if(isset($_POST['save'])){
//...
}
if(isset($_POST['delh'])){
//...
}
An alternative method would be something like this.
This makes use of the fact that you can open and close PHP tags anywhere.
<?php if (isset($_POST["delh"])) { ?>
<p>Deleted!</p>
<?php } ?>
<form method="POST" action="">
Code:<input type="text" name="name">
<?php if (isset($_POST["save"])) { ?>
<input type="submit" name="delh" value="Delete" />
<?php } elseif (isset($_POST["delh"])) { ?>
<input type="submit" name="save" value="Save" />
<?php } ?>
</form>

GET method not registering?

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.

$_POST variable

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?

Categories