$_POST array "wildcard"?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

$_POST array "wildcard"?

Post 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!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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 () 
}
Last edited by JayBird on Tue Jul 18, 2006 10:18 am, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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);
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Sorry, i edited my previous post
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post by Ward »

Code: Select all

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

}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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?
Post Reply