Click to See Complete Forum and Search --> : What does this script mean:


theuedimaster
06-18-2004, 04:05 PM
$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}
else {
foreach ($_GET as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}


I have no idea about the logic of how this works, if anyone could explain to me about this script, PLEASE HELP!
This is a piece of the original script off of http://webdevfaqs.com/php.php#mailer

mwiggin1
06-18-2004, 05:17 PM
This is checking to see if a form was submitted to this page using the "GET" method or the "POST" method and then it is using the "foreach" method of the Array class to list all of the elements that were posted ... in this format:

Element_name:value

When form data is posted, it is posted as an associative array ... meaning that it is an array indexed by keywords like this example:

$HTTP_POST_VARS("name") = "jimbo"
$HTTP_POST_VARS("email") = "j@b.com"
etc ...

so using foreach:

"$_POST as $key" is set to the name or "index" (name, email) of the of the array element and "$value" is set to the value of the array element (jimbo, j@b.com)

get it?