Foreach element in $_POST?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Is there a way to go through each element in the $_POST supervariable?

I'm making a page with dynamic forms, so I don't know for sure how many
$_POST variables there will be (the number of textboxes, radio buttons,
etc changes).

How can I either count them or do a foreach loop to do this?


Re: Foreach element in $_POST? wrote:
Quoted text here. Click to load it


Easy, just treat $_POST as an array and use foreach.

Don't forget to make the post values safe before using them if they were  
input by the users...


Re: Foreach element in $_POST?

Carl wrote:
Quoted text here. Click to load it

Ok, thanks

Re: Foreach element in $_POST?

Following on from Carl's message. . .
Quoted text here. Click to load it
Further don't forget that elements of $_POST may be arrays in their own  
right. eg Checkboxes.  Here is a snippet of code as illustration

function POSTget($VarName,$tidy=TRUE){
# read POSTed value from HTTP_POST_VARS
# Normally we'll tidy up the input to remove leading and trailing spaces  
and control chars
# but this can be overridden.
   if (isset($_POST[$VarName])){
     $rv = $_POST[$VarName];
       if ($tidy) {$rv = TidyInput($rv);}
   return $rv;

PETER FOX Not the same since the porcelain business went down the pan
2 Tees Close, Witham, Essex.
Gravity beer in Essex  <

Re: Foreach element in $_POST?

fun with forms... iterate_form() not only spits out the values, but
will give you the $var = $_POST['var'] code for the receiving page...
have fun

function iterate_form()
      echo "<b>Values:</b><br>";
      foreach($_POST as $key => $value)
        echo $key . " = " . $value . "<br>";
      echo "<br><br><b>Happy Coding:</b><br>";
      foreach($_POST as $key => $value)
        echo "$". $key . " = $_POST['". $key . "'];<br>";

    function build_back_form()
      echo "<form name='error' method='post' action='".
      foreach ($_POST as $key => $value)
        echo "<input type='hidden' name='" . $key . "' value='" .
$value . "'>\n";
      echo "<input type='submit' name='submit' value='Go

Site Timeline