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
action.php - I'm able to retrieve - $_POST

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

Any experts out there??

Recommended Answers

All 4 Replies

You cannot access $_POST 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).

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'];

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
3. $GLOBALS

PHP Source Code for global_session() is,

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

Thanks,

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.