954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I retrieve my form $_POST[] on multiple pages

Hi Guys,

I'm new to PHP and need help with passing variables from my form to multiple pages. I have global registers turned to off.

I am able to retrieve my form inputs on the second page.. however unable to retrieve the same on the other pages after.

for example -

form.php - my form page input - $_POST['startdate']
action.php - I'm able to retrieve - $_POST['startdate']

action2.php - I have a link on action.php which then redirects to this page; which again requires - $_POST['startdate'] but doesn't work, I tried validating and it did not return any value

Any experts out there??

xodus1
Newbie Poster
4 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

You cannot access $_POST['startdate'] in action2.php . Its only available in action.php since it is posted from form.php to action.php . In order to make it available in action2.php (which is a hyperlink), you have to pass it in the query string like this.

echo "<a href=action2.php?startdate=".$_POST['startdate'].">Click here to go to action2.php </a>";
//You can then access startdate in action2.php as $_GET['startdate']

If you don't want to pass it in the query string, you can use sessions (But I think using sessions only for this particular variable is useless).

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Or if you don't like the ugly url when using hyperlinks then use sessions. To convert $_POST to $_SESSION simply use the following at the very top of your action.php page:

<?
session_start();
foreach($_POST AS $key => $val) {
$_SESSION[$key]=$val;
}
unset($key);
unset($val);


Then on every page that uses sessions, you need to place session_start(); on the second line with no output before it. And to retrieve a session use the following.

<?
session_start();
//post variable was $_POST['name']
echo $_SESSION['name'];

//post variable was $_POST['age']
echo $_SESSION['age'];
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Hi,

It is very easy. In order to "register" or global $var variable, you can simply call global_session('var');

In doing so, you are able to access the same value in memory by the following 3 methods:

1. $var
2. $_SESSION['var']
3. $GLOBALS['var']

PHP Source Code for global_session() is,

function global_session($var){
        if(!array_key_exists($var,$_SESSION))
            $_SESSION[$var]='';
        $GLOBALS[$var]=&$_SESSION[$var];
    }


Thanks,

valsaraj
Newbie Poster
12 posts since Mar 2009
Reputation Points: 10
Solved Threads: 1
 

Thanks a million guys :) it worked perfectly, I prefer to use SESSIONS though..

xodus1
Newbie Poster
4 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You