MySQL Forums
Forum List  »  PHP

Re: How can I pass variable from one page to another
Posted by: Roland Bouman
Date: September 05, 2005 01:02AM

I'll say it again: read the php manual on this, there's no substitute:

http://www.php.net/manual/en/reserved.variables.php

I'll acmit it;s a bit tucked away, but the manual is the only real source.

If you are running php >=4.1.0, your login.php page can refer to these form inputs via the $_REQUEST[] array. This is 'superglobal': it is accessible from everywhere, and it's built in to, so you should not declare it yourself.
So within your login.php,

$_REQUEST['login'] would evaluate to the value in the input named "login", and $_REQUEST['Back'] would evaluate to the value in the input named "Back".

Be sure to use the same lettercase as you did naming the html inputs. A good way of handling this is to have a couple of defines:

define('INPUT_LOGIN','login');
define('INPUT_BACK','Back');

and reuse that in both scripts (possibly, put all those defines in a separate file that you include when you need it). Now, you could write:

<form action="login.php" method="post">
<input type="text" name="<?php echo(INPUT_LOGIN)?>" value="Log In">
<input type="text" name="<?php echo(INPUT_BACK)?>" value="Back">
</form>

and in the login.php page, you could write:

$login = $_REQUEST[INPUT_LOGIN];
$back = $_REQUEST[INPUT_BACK];

Another word about this.

If this is going to be an internet page, do not rely on people to visit login.php only via your login form. Consider what would happen if someone directly navigated to login.php. At the very least, your form input would not be posted, and this:

$login = $_REQUEST[INPUT_LOGIN];

would generate a php warning because the $_REQUEST array is empty. To handle this, I always use something like this (but I'm eager to hear what others are doing so please drop a line):

if (isset($_REQUEST[INPUT_LOGIN])){
$login = $_REQUEST[INPUT_LOGIN];
} else {
$login = NULL;
}


this way, i can always check for isset($login), which would be true only if it was present in the $_REQUEST array. Even if the input was empty in the form, it would still be present in the $_REQUEST array, having the value of the empty strin'g ('') wich is not the same as the PHP NULL constant.


I hope it helps, good luck!

Options: ReplyQuote


Subject
Written By
Posted
July 05, 2005 05:44PM
July 06, 2005 05:30AM
Re: How can I pass variable from one page to another
September 05, 2005 01:02AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.