I am wondering why: without clicking the submit button on a form on my PHP page, the _POST variable is already set.
For example, I have this piece of code on the web page:
if (isset($_POST)){
echo "XXXXXXX";
}
It turns out the XXXXXX is echoed just when the page loads the very first time -- at this point I have of course not submitted any data to the server using POST. Why would this be the case?
As specified on PHP.net, it is automatically created.
This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.
To address your code, it is created, but it's empty.
To better test if the user has made a POST request, simply test for an index on $_POST, like isset($_POST['someFieldName'])
Your code should test if it's empty or not:
if(!empty($_POST)){
echo "a";
}
or
if(isset($_POST['someFieldName'])){
echo "a";
}
$_POST is a superglobal array and it is always set. If you do not pass any value to it using post method it will be empty. So set a value to this array and check whether that value is available or not like this:
if(isset($_POST['submit'])){
//Do this...
}
The same is true for the $_GET superglobal. These are always set regardless of HTTP Method. You can check if a request was a POST, GET, PUT, etc by checking the REQUEST_METHOD
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// this was an HTTP POST request
}
Check if the variable is empty or not. $_POST will always be set.
So something like the following should work:
if(!empty($_POST['textFieldName'])){
echo "XXXXXXX";
}
$_POST is superglobal and it's always set as an empty array.
Try this, just to understand better:
if(!is_null($_POST))
{
print_r($_POST);
}
Why is this going to help you to understand? - Because isset checking if a variable is set and is not NULL.
Related
I was trying to pass an error message to another page using session variable. On the handler page, the session was initialized in the manner:
function sess() {
$index = $_POST['name'];
$_SESSION['error'] = ($index == '') ? 'name cannot be empty' : $index . " is a good name";
header("Location: http://website-name.com/form");
}; sess();
and on the other page is something like:
if(isset($_SESSION['error'])) {
echo $_SESSION['error']; unset($_SESSION['error']);
};
Now if the form is submitted with an empty field, the error name cannot be empty becomes the value of the session. After that, if the name is then set, the value of the session doesn't change but still returns name cannot be empty.
I've checked to see if it has something to do with the fact that the session was called within a function. got no idea! but found online a way to unset all variable using session_unset. But it's actually one session variable that i want to unset and i found something like session_unset($_SESSION['error']). However, using it did not unset all session but the $_SESSION['error'] doesn't get set anymore within the function unless i use the unset($_SESSION['error']) which doesn't terminate the first session that is set.
So what is actually going wrong? how does unset_session() works differently form unset()? what does it has to do with being inside a function? As far as i can recall, $_SESSION[] is a global variable just like $GLOBALS[] which can be access in any scope.
I'm sure this is a rookie question but I get undefined index in the $_POST password parameter.
if ($_SESSION['Password'] == $_POST['Password']){
echo 'Hi 2 '.$_SESSION['Password'].'<br>';
}
If I do the following with either with $_SESSION or $_POST, I get the correct echo line.
if (!empty($_SESSION['Password'])){
echo 'Hi 2 '.$_SESSION['Password'].'<br>';
}
Can it be that same parameters cannot be used for both variables ?
First you should understand what is SESSION and POST variables.
SESSION is used to store a session variable that can be accessed throughout the session from any PHP document. POST is used to get any variable that send through the post method.
In the first code snippet you are checking weather the
SESSION['Password'] == POST['Password']
and you are not providing any value through post method and the post variable 'Password' is not defined in your program to that's why you are getting an undefined error.
So to over come this one the best way is to check weather both of the variable has defined or not by using isset function.
if(isset($_SESSION['Password']) && isset($_POST['Password'])){
if ($_SESSION['Password'] == $_POST['Password']){
echo 'Hi 2 '.$_SESSION['Password'].'<br>';
}
}
Provide the value for POST['Password'] through a correct statement then the code will work.
I think you know how to set values for SESSION variable because you are comparing a SESSION variable with a POST variable.
I'm using $_SESSION for the first time. I think I get the concept, but something isn't working. This is a page that need to load once with POST variables and then, when reloaded (to page through search results), remember the values of the post variables. The two variables would always be set or not set at the same tiem.
//submitted form variable definitions
if (!isset($_SESSION)){
session_set_cookie_params(3600,"/");
session_start();
}
if (isset($POST['word'])) { $name=$_POST['word'];
$_SESSION['word'] = $name; };
if (isset($POST['exact'])) { $exact=$_POST['exact'];
$_SESSION['exact'] = $exact; };
Your POST variables have a mistake. It should have an underscore _ like follows.
isset($POST['word']) // change this
isset($_POST['word']) // to this
isset($POST['exact']) // change this
isset($_POST['exact']) // to this
Otherwise it will always return false.
It is also better to use session_start(); at the top (It is not a problem here).
To further elaborate: $_POST is a superglobal. All except $GLOBALS require an underscore between $ and the method used.
These superglobal variables are:
$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION
$_REQUEST
$_ENV
as per the manual:
http://php.net/manual/en/language.variables.superglobals.php
I am developing a php script which contains an html form.
If not all fields are filled in the correct way the script will signal an error and redirect back to the same page with the header function setting an error variable to yes with the get method:
header("Location: registration_page.php?error_empty=yes");
my script has an error handling part in which it highlights the fields containing a mistake, but I would like to keep the value of the fields correctly filled.
I am implementing this feature as I found in this other question:
How can I keep a value in a text input after a submit occurs?
but the problem is that when the page reopens the forms will not contain the old values.
My question is: does anybody know if the header function unsets global variables in the $_REQUEST array?
And do you know what kind of solution could I adopt?Maybe sessions?
Thanks in advance,
Matteo!
$_COOKIES will stay set, but $_POST & $_GET will be destroyed, as the client is moving to a new page. If they need to be retained, they must first be stored into $_SESSION before calling the redirect.
session_start();
$_SESSION['last_post'] = $_POST;
header("Location: http://example.com");
exit();
// On the redirected page, use the stored POST values and unset them in $_SESSION
session_start();
if (empty($_POST) && isset($_SESSION['last_post'])) {
$post = $_SESSION['last_post'];
unset($_SESSION['last_post']);
}
else $post = $_POST;
Does anybody know if the header function unsets global variables in the $_REQUEST array?
No, it doesn't. Cookies ($_COOKIE) will remain.
Obviously $_GET will contain whatever you have in the redirect (eg: $_GET['error_empty'] = 'yes') and$_POST` will be empty because you aren't posting.
So it $_REQUEST will be a combination of $_COOKIE and the new $_GET parameters you set.
You probably shouldn't be using $_REQUEST anyway. Specify exactly where you expect your request parameters to be...
It doesn't, but it does make a new request. A new request means a new $_REQUEST which does not necessarily have all of the old data. $_COOKIE will still be there but $_GET and $_POST will be new meaning that $_REQUEST will reflect that. ($_FILES will also be empty and while it is not in `$_REQUEST, it is another user-supplied value will be reset).
If you want to restore the form, you'll need to either put all of the variables in the url in the header, or use $_SESSION or setcookie and then restore from $_GET, $_SESSION or $_COOKIES.
Once some data are submitted through POST, is it possible to make them available as $_POST through different pages, same like how $_SESSION allows us to do?
You'll need to parse the data in $_POST and recreate it in your form. You can do it with hidden fields.
Or, you can save the $_POST data in a user session and refer to it when you need it. You'll have to manage the lifecycle of the data to make sure it doesn't stay around too long.
Digging up an old question today. But i forgot to post the working solution I dug....
Place this snippet in top of your every page
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
The problem is that you are sending the data in via the URL which is stored in the $_GET variable, not in $_POST. If you want the ability to submit the data in either format, use $_REQUEST instead.
There is a bit of a debate on whether it is a good idea to use $_REQUEST, but if you are doing a simple site, there is little wrong with it.
If you would rather not use $_REQUEST, then you can use the following code on each variable you are expecting:
if (!empty($_GET['foo'])) {
$foo = $_GET['foo'];
} elseif (!empty($_POST['foo'])) {
$foo = $_POST['foo'];
} else {
die("Foo not submitted");
}