Your open source for Development and Design

PHP foreach loop through post request

Filed under: PHP — Tags: , , , , — Adi @ 3:48 pm January 14, 2009

I use a variation of this code on most websites I have coded as it is versitile and full of uses when working with dynamic content. An example might be reusing the same mail() function for differant forms, you could loop through what is posted to it and print it out in the body of the mail.

The foreach loop will process each of the requests set to the page. The $_REQUEST can be changed to just $_POST to check only posted form data or any other type of request.

The following loop contains a few examples of what you could do with the request once you have it.

foreach ($_REQUEST as $key => $value)
{
$value = mysql_real_escape_string( $value );
$value = addslashes($value);
$value = strip_tags($value);
if($value != ""){$requestnumber ++;}
echo $key. ' - '.$value.'</br>';
}

Switch to our mobile site