PHP help - user defined variables
PHP help - user defined variables
Author
Discussion

KITT

Original Poster:

5,345 posts

271 months

Tuesday 20th December 2005
quotequote all
I need to create a form to post to another page containing all the user defined variables. Best I can come up with at the moment is this:
php said:

$array=$GLOBALS[GLOBALS];
foreach ($array as $i => $value) {
echo "< input type=hidden name=$i value=$array[$i] >\n";
}
Trouble is this includes EVERY variable including server and environment ones. Anyone know how to just get the user defined ones?

cheers

page3

5,197 posts

281 months

Tuesday 20th December 2005
quotequote all
I'd probably write them to a database and then use a session id to track and re-load them.

Alternatively you could use a cookie.

Or, you could filter out unwanted elements:

php said:

//get variables
$passedVariableNames = array_keys( $_REQUEST);
//reduce them to only those we want
$passedAttributeNames = array_filter( $passedVariableNames, "ItemMatchesAttribute" ) ;

//now walk through the array
foreach( $passedAttributeNames as $attributeKey=>$attributeName) {
// get the attribute id from the name
$attributeNameItems = explode( $servSeparator, $attributeName);
// the id will be the second item in the array
$id = $attributeNameItems[ 1];
etc...

Defined ItemMatchesAttributes thus:

function ItemMatchesAttribute( $item)
{
global $attributePrefix;

// check if the item starts with the attributeprefix
return ( substr( $item, 0, strlen( $attributePrefix)) ==$attributePrefix);
}

Where you have defined attributeprefix something like:
$attributePrefix = "ServAttr";


..if you follow me.


>> Edited by page3 on Tuesday 20th December 10:07

KITT

Original Poster:

5,345 posts

271 months

Tuesday 20th December 2005
quotequote all
Trouble is $_REQUEST only gives the passed variables (form the previous pages POST request) and the function ItemMatchesAttribute looks to be comparing the variable name with whatever $attributePrefix is defined as. I don't know what the names are of the defined variables and hence can't do a comparison.

Closest I can get it to use $GLOBAL and create a list of ignorable names like GATEWAY_INTERFACE, SERVER_SOFTWARE etc.

Any more suggestions welcome

page3

5,197 posts

281 months

Tuesday 20th December 2005
quotequote all
KITT said:
Trouble is $_REQUEST only gives the passed variables (form the previous pages POST request) and the function ItemMatchesAttribute looks to be comparing the variable name with whatever $attributePrefix is defined as. I don't know what the names are of the defined variables and hence can't do a comparison.

Closest I can get it to use $GLOBAL and create a list of ignorable names like GATEWAY_INTERFACE, SERVER_SOFTWARE etc.

Any more suggestions welcome
What I do is name all my form variables ServAttr_XXX where XXX changes. My filter function therefore removes all variables that don't start with this and then I explode it (on the '_') to get my proper variable name/value pair. Works for me I might be misunderstanding you though or this might not work for you.

KITT

Original Poster:

5,345 posts

271 months

Tuesday 20th December 2005
quotequote all
Ah I see.

I'm actually writing some code that other web programmers will include in their own code and it needs to be able to pass on any defined variables to another page. Hence I won't know what variables have been defined by the other developer(s). I've created a solution where they use fill an array with the names of their defined variables so I can ensure they get passed on. It works for now anyway!

cheers

TheExcession

11,669 posts

280 months

Tuesday 20th December 2005
quotequote all
Can you just forward the $_POST vars? That way you only send what ever came through from the original form?

www.w3schools.com/php/php_forms.asp

If not, how are these User Vars created?

best
Ex

KITT

Original Poster:

5,345 posts

271 months

Tuesday 20th December 2005
quotequote all
php said:
if ($RandomVar=="" )
$RandomVar=GetValueForRadomVar();
//KITT's code here:
include('kittscode.inc');
Now, kittscode.inc needs a way of knowing about $RandomVar so that when the page is reloaded with a POST form, $RandomVar has the original value.

>> Edited by KITT on Tuesday 20th December 16:17