count POST parameters [Solved]

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
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

count POST parameters [Solved]

Post by sarris »

Hi there,
I am calling this php script which is not called always with the same ammount of parameters. Is there a way to get all the POST parameters in an array or in a way iterate through them so i get them all every time?

Thanks
Last edited by sarris on Wed May 09, 2007 7:42 am, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

I'm not entirely sure what you mean.
You can count POST parameters quite easily:

Code: Select all

$number = count($_POST);
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

As for stepping through them...

Code: Select all

foreach ($_POST as $key=>$value) {
  echo "POST input {$key} has a value of [{$value}]<br />";
}
the $_POST is a "Read only" array although you cannot change any values you can perform most array functions on it.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

CoderGoblin wrote:the $_POST is a "Read only" array although you cannot change any values you can perform most array functions on it.
This script disagrees:

Code: Select all

<?php
print "<pre>";
print_r($_POST);
$_POST['bob'] = "changed!";
print_r($_POST);
print "</pre>";
?>
<form method="post">
<input type="text" name="bob"  />
</form>
Output is something like

Code: Select all

Array
(
    [bob] => cake
)
Array
(
    [bob] => changed!
)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

My mistake :oops: ... Wouldn't recommend it however.
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

great,
thanks
Post Reply