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.
Related
I currently have some php code on a form action, and it is being updated to the form's next page with
<?php echo $_POST['var here']; ?>
and it is working, but I noticed when Im trying to refresh the page it asks to confirm resubmission. When I resubmit it works in that tab in which it was sumbitted, but in another new tab it does not show the displayed php post variables. I even took it the next step by seeing that when I open the 2nd page after the form action has been submitted the php post variables are gone...
Help!
Thanks!
When you submit a form with <form method="post" /> it does a post request to the server, thus populating $_POST. When you open the link in a new tab it is no longer a post request but a get request. That is why you'll see nothing in $_POST.
$_POST — usually from forms
$_GET - from values on the URL (the query string myscript.php?myvar=Joe)
You can find plenty of resource about it. You can start here
If you want to keep the values you can save them to the session:
<?php
session_start(); // should be at the top of your php
if (isset($_POST['var'])) {
$_SESSION['var'] = $_POST['var'];
}
$myvar = isset($_SESSION['var']) ? $_SESSION['var'] : "no var";
echo $myvar;
Now the value is stored in the session so you can visit the page in a new tab and it will still be there.
This sounds like desired behavior. The $_POST variable should only be filled when the post action is created. If you're looking to store variables across pages you could store it in either the $_SESSION var in PHP or deal with the front end $_COOKIE business. If you're always going to be rendering pages from the backend then $_SESSION is the way to go. It's never too late to read up on cookies and sessions.
The skinny of it is that you're going to want to do something like this:
<?php
session_start();
if ($_POST['var']) {
$_SESSION['var'] = $_POST['var'];
}
echo $_SESSION['var'] ?: $defaultValue;
Then you'll notice that the message changes only when you post and won't exist before then.
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.
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
Can some explain to me the best way to store a $_GET variable in a session and the only way the sessions changes is when we verify the data the session is being change to is different from the GET variable.
Currently i have
$tid = clean_get($_GET['tid']);
in a global file which is included on every page the problem with that is the value of $tid will be erased and not stored in a session like i want it to once the user is not on a page with $tid set in the url.
If you get $_GET['tid'] in url then set session again by that new value otherwise restore it from session. Thats it.
session_start();
$tid = (isset($_GET['tid']) && $_GET['tid']!="") ? clean_get($_GET['tid']) : $_SESSION['tid'];
Try this and tell me is it solved?
Use a function like isset() to see if it is being sent. Only then should you replace it:
if(isset($_GET['tid']))
{
$tid = clean_get($_GET['tid'])
// Do stuff to change session data.
}
I think what you are looking for is something like
session_start();
foreach ($_GET as $key=>$value) {
$_SESSION['getValues'][$key] = clean_get($value);
}
This will store all the values in $_GET in the $_SESSION. To retrieve the values later, you just have to use $_SESSION['getValues']['tid'] after calling session_start().
Here I'm assuming that clean_get() is just something that formats and/or escapes data that came in from forms, so calling it on each value before sticking into the session will do all that cleaning when needed.
Note: only call session_start() once, and make sure you do so before doing anything with $_SESSION, otherwise you'll get error messages.
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");
}