Page 1 of 1

$_POST array "wildcard"?

Posted: Tue Jul 18, 2006 10:01 am
by mattcooper
Hi all,

I'm trying to write a generic function to process each element from a $_POST array. I am having difficulty with this using the following code:

Code: Select all

function get_Posts ( ) {
		
		// Grab $_POST data for each textfield and replace relevant parts of XML doc
		$post = array($_POST);
		foreach ( $post as $posted ) {
			echo $posted['WHAT DO I PUT HERE?']."<br />\r\n";
		}
	} // END FUNCTION get_Posts ()
Because the $_POST is an array, the code obviously won't work... Aaargh. How is this done?

Thanks!

Posted: Tue Jul 18, 2006 10:09 am
by JayBird

Code: Select all

function get_Posts ( )
{              
	// Grab $_POST data for each textfield and replace relevant parts of XML doc
	$post = $_POST;
	
	foreach ( $post as $k=>$v )
	{
		echo $post[$k]."<br />\r\n"
	} // END FUNCTION get_Posts () 
}

Posted: Tue Jul 18, 2006 10:17 am
by Burrito
I can't say I've ever seen an array 're-declared' like that.

I guess I could test it, but I"m too lazy.

you could always pass the post array as an argument to your function...ie:

Code: Select all

function get_Posts($arr)
{
  foreach($arr as $k => $v)
  {
     echo $v."<br />\r\n"; // this is the same as $post[$k]...they both return the value
  }

}

get_Posts($_POST);

Posted: Tue Jul 18, 2006 10:18 am
by mattcooper
Yes, I had tried that... heres the code for the form:

Code: Select all

<form action = "<? echo $_SERVER['PHP_SELF']; ?>" method = "post">
<input type = "text" name = "width" id = "width" value = "<? $width = 100; echo $_POST['width']; ?>">
<input name="submit" type="submit" value="Submit">
</form>
So, there's currently only one field. The output from the new piece of PHP followed by a print_r($_POST) is:

Code: Select all

Array
Array ( [width] => 987654321 [submit] => Submit )
So, using $k still only outputs "array", but I'm looking for "987654321".

I say again... Aaaaargh!

:D

Posted: Tue Jul 18, 2006 10:19 am
by JayBird
Sorry, i edited my previous post

Posted: Tue Jul 18, 2006 10:21 am
by Ward

Code: Select all

$keys = array_keys($_POST);
foreach($keys as $key)
{
    print $key." = ".$_POST[$key]."<br>";

}

Posted: Tue Jul 18, 2006 10:23 am
by Burrito
no using '$k' in my example would output the value of all of the keys in the $_POST array.

try the code I posted exactly and see what you get.

Posted: Tue Jul 18, 2006 10:32 am
by mattcooper
Yep, looked fine, but needed filtering to get rid of "submit" from the button:

Code: Select all

function get_Posts($arr) { 

		foreach($arr as $k => $v) {
   
			if ($k!=="submit") {
				echo $v."<br />\r\n"; // this is the same as $post[$k]...they both return the value 
				echo $k;
  			} 
 		}
	}
Thank you, my mental block has now evaporated and I shall continue to exercise the grey matter.

Anyone heard of a PHP admin interface for the "Flipping Book" flash component, BTW?