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.
Related
<?
session_start();
$_SESSION['name'] = "$_GET["name"]";
$_SESSION['email'] = "$_GET["email"]";
$session_id=session_id();
echo"$session_id <br> $_SESSION['name'] <br> $_SESSION['email']";
?>
I am trying to create a session to store visitor input form with GET method, I cant use POST because the form is handled by wordpress plugins and the client only gave me the GET option. The problem is:
On page #1, this is the page after we submit the form, the echo is shown complete.
At page #2, I already add session_start(); at top but $_SESSION['name'] and $_SESSION['email'] keep empty (change page) but $session_id is stored and show same.
What am I missing? or maybe $_SESSION is cant store $_GET?
So on the first page you save the values from the user input and it works as expected.
However, when you go to the second page, your code is overriding the session with new variables, which are empty in this case. Before assigning anything to the session, you should check whether it is not empty / valid.
For this simple code I would recommend checking via isset / array_key_exists functions.
I'm having a very weird SESSION problem, I've been working with sessions in my server for a long time and everything working fine. I've searched & tried a lot of changes for 5 hours before posting this question.
Anyway, it seems that $_SESSION is not updating correctly. I have a form using POST that goes to the same page and when the user submits the info its saved in $_SESSION and then I do some stuff and update the session variable. Everything looks good in the page cause when I'm done I save the session with the new variables and then do a var_dump($_SESSION["whatever"]); and it shows the updated data. But when I submit the form again with new settings and show the data of $_SESSION in the reloaded page it's still the old data.
My code is kind of complex and I do a lot more than just save the data but I'm resuming the situation here:
session_start();
var_dump($_SESSION["whatever"]);
if(isset($_POST["whatever"])){
$_SESSION["whatever"]=$_POST["whatever"];
} else {
$_SESSION["whatever"] = FALSE;
}
var_dump($_SESSION["whatever"];
So, the below var_dump does show the new value entered in the form, that should mean it has been saved correctly, but no! Because when I enter the form again the first var dump that should have the same value as the last var_dump in the previous page still shows the original value (FALSE).
Any ideas?
UPDATE: Even if I go to a new page and just do this:
session_start();
var_dump($_SESSION["whatever"]);
It shows the old data, not the updated one.
PS: By the way I have many other variables that are not inside this if/else statement that do save correctly, and the session does update from the values on other previous page.
NEW UPDATE: I'm using jQuery to display the form with sliders and knobs, if I eliminate jQuery everything works well! It is very weird! I have seen nothing like this on the internet, any ideas?
session_start();
var_dump($_SESSION["whatever"]);
if(isset($_POST["whatever"])){
$_SESSION["whatever"]=$_POST["whatever"];
} else {
// Check if there is a session variable set
if (!isset($_SESSION['whatever'])) {
// If the session variable isn't set, initialize it to false
$_SESSION["whatever"] = FALSE;
} else {
// Set the $whatever variable to be the value of the session variable
$whatever = $_SESSION['whatever'];
}
}
var_dump($_SESSION["whatever"];
<!-- In the HTML -->
<input name="whatever" value="<?php echo $whatever ?>" type="text">
I'm trying to send the value of this php get_field variable from a wordpress Archive page.
get_field('supplier_email_address', 'product_brand_' . $term->term_id );
to another sendmessage.php page which is never accessed other than with the of JS to send a form. The variable I need filled is called:
$sendto="value";
How can I achieve this?
EDIT: Apparently this requires more information.
Use session variables.
First of all every page you want to use sessions on needs to have the function session_start(); declared at the top of every page. You then can use the session superglobal to store things.
Pg1.php:
<?php
session_start();
$_SESSION['variable_name'] = 'string';
?>
Pg2.php:
<?php
session_start();
echo $_SESSION['variable_name'];
?>
Pg2.php will show: some string
Either POST your value so your second page can retrieve it in the $_POST array, or include it in the URL path so your second page can $_GET it.
Without more info, we can't really help you any more than that.
I have a login form which sends 3 post values from username, password and submit button. But my form processor has 3 pages one is validation.php which validates the field second is read.php which checks the posted values against db and third is login.php which is a result of login success. All redirect to each other respectively on success. Problem here is that when I try to access the user posted values from form in read.php (redirected page) not validate.php (action page) I get an error of undefined index.
I really don't see why you are doing all those redirects, but if you want to make the data more persistent you could use a session variable, because the $_POST superglobal is only set for the current request.
firstfile.php
<?php
session_start();
$_SESSION['posted_data'] = $_POST;
other file
<?php
session_start();
var_dump($_SESSION['posted_data']);
However as already stated you may really want to reconsider doing all the requests.
UPDATE
Besides the fact that you will loose your data you are also doing multiple (unneeded) requests to simply sumbit the form. The only redirect that should happen is to the successpage when you have done all you work. See this for more information: http://en.wikipedia.org/wiki/Post/Redirect/Get
If you are look to keep you code clean you could always just include the other files or go for an OOP approach.
You should do one page only that will do all the work. That doesn't seem too complicated of a script, so I would advise putting everthing together on one page.
You did not provide any code so I'll show you a general example. I just typed it without rereading so it's not pure PHP syntax, just the spirit:
<?php
$login=$_POST['login'];
$pwd=$_POST['pwd'];
$dbcheck = mysql_fetch_array(mysql_query("SELECT COUNT(1) FROM table WHERE user =$login and pwd = $pwd"))
if($dbcheck[0] > 0) {
//Login success
//Setup your session variables, cookies, etc
//Then you can do your redirect here
} else {
//Page for wrong login
}
I want to send a feedback message from one page to another without using input elements via POST method. Is that possible? Or is there other way around?
What i want is applying a value from one page to another page's variable.
i.e.: Sending "Yaba Daba Doo" string from 1.php to 2.php's variable, named $info.
1.php:
<?php
$info = "Yaba Daba Doo";
?>
2.php
<?php
$variable=$_POST['info'];
echo "The message is:".variable;
?>
How can i pass info variable to 2.php using POST method, without input elements?
You can use session variable to store data on server and access across the pages of your application.
If you want a secure way to send this variable you can use session so that end user can not see it
An example
page1.php
session_start();
$_SESSION['mes']=$info;
page2.php
session_start();
echo $_SESSION['mes'];
And the another way is to pass variable in GET which I will not recommend user can see the value of your input variable
Use a session to store your data, then when ever you want to store is call upon the $_SESSION global.
Here is more info about sessions:
php.net
W3schools
Tutorial
you can use php curl to make a post request to 2.php from 1.php