Click to See Complete Forum and Search --> : i dont get this


soccer362001
12-16-2003, 01:03 PM
Im trying to learn php i cant figure out what all of this is. I look at php.net and cant seem to find any thing that has to do with it.

<?PHP

#######################################################
# This script is Copyright 2003, Infinity Web Design #
# Distributed by http://www.webdevfaqs.com #
# Written by Ryan Brill - ryan@infinitypages.com #
# All Rights Reserved - Do not remove this notice #
#######################################################

## The lines below need to be edited...

###################### Set up the following variables ######################
#
$to = "you@your.com"; #set address to send form to
$subject = "Results from your Request Info form"; #set the subject line
$headers = "From: Form Mailer"; #set the from address, or any other headers
$forward = 0; # redirect? 1 : yes || 0 : no
$location = "thankyou.htm"; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################

## set up the time ##

$date = date ("l, F jS, Y");
$time = date ("h:i A");

## mail the message ##

$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";
}
}

mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}

?>

pyro
12-16-2003, 01:17 PM
What part are you questioning?

soccer362001
12-16-2003, 01:34 PM
from set up time down. I dont get how it works.

pyro
12-16-2003, 01:55 PM
The $date and $time are set up using the date (http://us2.php.net/date) function to format the timestamp.

Next, we are checking if the request method was POST (ie whether they set the method of the form to post). If it returns true, we know they set it to post, so we use a foreach loop to loop through each POST variable. The $key will be the name of the POST variable (and thus, the name of the form field), and the $value will be the value of the POST variable (and thus, what the user entered in the form field). If the method was not post, we do the same for GET.

After that, we simply mail (http://us4.php.net/mail) the message to the desired address, and forward them to the location set above (if any).

soccer362001
12-17-2003, 05:36 PM
what would i need to do to check to see if the values are empty?

I found this but i dont know where to put it.
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}

pyro
12-17-2003, 06:13 PM
Something like this (add somewhere near the top of the script -- above where it would email the message).

if (empty($_POST['foo'])) {
echo "You left the field 'foo' empty. Please fill it out.";
exit(); # stop processing
}

soccer362001
12-18-2003, 08:00 AM
thanks it works

pyro
12-18-2003, 08:09 AM
You're welcome. :)