Page 1 of 1

Finding the name(s) of form data (esoteric?)

Posted: Tue Mar 29, 2005 6:59 pm
by robster
I have a question regarding a form i have created using a loop.

This form has text boxes that are generated with each pass through the loop. To differentiate the text box names, I have used this code:

Code: Select all

<td class=\"clientview\"><div align=\"center\"><input name=\"stock_price".$stock_id."\" type=\"text\" value=\"$stock_price\" size=\"4\" maxlength=\"4\"></div></td>
The $stock_id is a different value each time through the loop and gives, as an example, output similar to this (assuming three times through the loop):

Code: Select all

stock_price11
stock_price2
stock_price43
The question now, is when I post (or get ;)) that form data, how can I use a $_Get on the next page to retrieve the data, when the names could be different each time?

I can't use:

Code: Select all

$stock11 = $_GET['stock_price11'];

$stock43 = $_GET['stock_price43'];
because depending on what the user has picked on the previous page, it could come through as something different and that code would then not work (I hope that made sense).

Does anyone have any ideas on how to actually find the name of the variables that are coming through (as well as their contents) so I can place them in variables?

I'm guessing, using pseudo code, it would look something like this:

Code: Select all

-look through post data
-find names (somehow :))
-use names to create a $_Get statement and assign variables
Using that concept, variable 'stock_price11' with a value of '38' (for example) could then be called $stock_price11.

I think you get me.

Thanks for this, it's a real stumper and way beyond my current knowledge of form data handling.

Posted: Tue Mar 29, 2005 7:11 pm
by feyd

Code: Select all

'name=&quote;stock_price&#1111;' . $stock_id . ']&quote;'

re:

Posted: Tue Mar 29, 2005 7:32 pm
by harrisonad
try this:

- get all defined variables

Code: Select all

$vars = get_defined_vars();
- get the array for POST variables

Code: Select all

$postvars = $vars['_POST'];
- browse through the array values and select only those with keys that starts with 'stock_price'

Code: Select all

foreach($postvars as $key=>$value){
if($substr($key,0,10)=='stock_price'){
// do stuff here ...
}
}
well, that's it! i hope it will solve your problem.


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Mar 29, 2005 9:08 pm
by robster
Thanks so much for that. I decided to go through your code and pull what was needed (as it didn't work exactly as was displayed, most likely though my own ignorance ;)).

This is what I ended up with, and it works a treat. Really has helped me a lot, and in playing with the get_defined_vars() I really found a lot of good stuff in there that should not have been floating around for prying eyes. A worthy security check you might say.



Code: Select all

//get all defined variables 
		$variables_array = get_defined_vars();
		
		
		foreach($variables_array as $key=>$value)
		{
			//Deal with the SERVICE data
			if (substr($key,0,13) == "service_price")
			{
				//Strip not needed data from the key variable
				$new_service_key = substr($key,13);
					
				//Display the data, just to be sure 
				echo "<strong>Service</strong> ID: $new_service_key - Price: $value <br>";
				
				//Do database manipulation here
				
			}
			
			//Deal with the STOCK data
			if (substr($key,0,11) == "stock_price")
			{
				//Strip not needed data from the key variables
				$new_stock_key = substr($key,11);
				
				//Display the data, just to be sure 	
				echo "<strong>Stock</strong> ID: $new_stock_key - Price: $value <br>";
				
				//Do database manipulation here
				
			} 

		}
This outputs:

Code: Select all

Service ID: 7 - Price: 70
Service ID: 2 - Price: 38
Stock ID: 32 - Price: 22.9
Stock ID: 11 - Price: 18.9
Stock ID: 29 - Price: 17.5

Posted: Wed Mar 30, 2005 12:20 am
by php_wiz_kid
Here's my approach:

Code: Select all

$GetVars = get_defined_vars();
$ArrayKey = array_keys($GetVars);
for($Looper = 0; $Looper <= (count($ArrayKey)-1); $Looper++) {
	if($ArrayKey[$Looper] == "_GET") {
		foreach($GetVars[$ArrayKey[$Looper]] as $key => $value) {
			if(!isset($_GET[$key])) {
				$_GET[$key] = null;
			}
		}
	}
}