PHP foreach loop through post request

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn

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>';

Posted by Adi on January 14, 2009

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn

One response to “PHP foreach loop through post request”

  1. Bruce says:

    Thank you! One note for anyone else who comes across this: mysql_real_escape_string fails if you have not opened a mySQL connection. So, you do not open a mySQL connection before the above code runs $value becomes empty when line 03 runs.

Leave a Reply

Your email address will not be published. Required fields are marked *