Header Image

PHP FAQs - Answered!

Content Listing:

How do I get form results emailed to me?

You will need to use PHP's mail() function. A basic mail() tag would look like this:

<?PHP mail("toaddress", "subject", "message body", "additional_headers"); ?>

Example - A sample form with a form handler:

The form Code:

<form action="mailer.php" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
Message: <textarea name="message"></textarea>
<input type="submit" name="submit" value="Submit Form">
</form>

So, now you have the HTML part of your form done. Let's explain the important part. The forms action (action="mailer.php") needs to point to the PHP file. This tells the form where to go once it is submitted. We want it to point to the file that we will create below. This script now supports the GET method, so you no longer need to use the POST method, but it is still recommended to use the POST method, as this will help ensure the security of the data.

Here is the PHP code to process your forms, and mail the results back to you:

<?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.";
}

?>

Download this file with a sample form.


Why don't my variables work?

Since PHP 4.2.0, global variables default to "off" in the php.ini file. Many books/tutorials were written prior to this change, so even if you copied the script exactly as you saw it, it may not work. How do you fix it? You must now call the variables from the superglobals array. So, for forms sent with the POST method, you can reference variables with $_POST["var"].

Here is a list of all the superglobals:

$GLOBALS Contains a reference to every variable which is available in a global scope.
$_SERVER Variables set by the web server or otherwise directly related to the execution environment of the current script.
$_GET Variables provided to the script via HTTP GET [used to get values out of the URL.
$_POST Variables provided to the script via HTTP POST [used for forms].
$_COOKIE Variables provided to the script via HTTP cookies.
$_FILES Variables provided to the script via HTTP post file uploads.
$_ENV Variables provided to the script via the environment.
$_REQUEST An associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.
$_SESSION Variables which are currently registered to a script's session.

Take a look at this example.

<form action="handler.php" method="post">
Enter a variable: <input type="text" name="phpvar">
<input type="submit" name="submit" value="Submit Form">
</form>

And the PHP page (handler.php)

<?PHP
echo $phpvar; # That will not work
echo $_POST["phpvar"]; # That will work
echo $_POST['phpvar']; # That will also work
?>

And one more example. Say you want to send a value to another page via the query string.

<a href="yourpage.php?var=yourvalue">send value to page<a>

And the PHP page (yourpage.php)

<?PHP
echo $var; # That will not work
echo $_GET["var"]; # That will work
echo $_GET['var']; # That will also work
?>


How can I get a visitor's IP address?

You need to use the predefined variable $_SERVER['REMOTE_ADDR'], which can be sent to the HTML page easily like this:

<?PHP echo $_SERVER['REMOTE_ADDR'] ?>

For more information, see the PHP documentation. Note that in versions of PHP prior to 4.1.0 you will need to use $HTTP_SERVER_VARS instead. Note also that since IP address can change (and usually do, unless the user is on an always-on connection) you cannot rely on an IP address uniquely and permanently identifying a user.

In case you don't have PHP available, you can do the same thing in SSI:

<!--#echo var="REMOTE_ADDR"-->

Or ASP:

<%Response.Write(Request.ServerVariables("remote_addr"))%>

How do I include content on all my pages?

To keep pages more manageable, it is often nice to use includes to include blocks of code that remain the same from page to page. To do this, we want to use PHP's include function.

<?PHP
include "include.php";
?>

For more information, see http://us4.php.net/manual/en/function.include.php.

In case you don't have PHP available, you can do the same thing with SSI.

Use this one, if you don't need to include the path:

<!--#include file="include.txt"-->

Use this one, if you need to include the path:

<!--#include virtual="path/to/include.txt"-->