How to Create a PHP Web Form
PHP is a server side scripting language that is used in conjunction with HTML to create dynamic web pages. When used within a form, information can be obtained from web page and then processed in ways that cannot be accomplished with HTML alone. These forms can process log-in information, complete calculations and create interactive websites.
This article will show you how to create a PHP script that allows a user to email you directly from your web page.
Instructions
-
-
1
Create the form in HTML:
<form action="emailme.php" method="post">
Subject: <input type="text" name="subject">
Email: <br />
<textarea name="body"></textarea><br />
Your Name: <input type="text" name="name"><br />
Your Email Address: <input type="text" name = "addy"><br />
<input type="submit" value="SEND">
</form>In the first line, the action tells the server what to do with the form. The method tells it that you are going to use the PHP $_POST function.
-
2
Declare the variables for the PHP script:
<?
$subject = $_POST['subject'];
$body = $_POST['body'];
$name = $_POST['name'];
$addy = $_POST['addy'];
$to = "user@email.com";$subject is the subject of the email.
$body is the body of the email.
$name is the name of the person sending the email.
$addy is the email address of the person sending the email.
$to is the email address the email will be sent to. Change 'user@email.com' to reflect your email address.
$_POST is a PHP global variable that requests the form parameters without changing the URL. You could also use the global variable $_GET, but that would place the contents of the variables into the URL.Each statement in PHP must be followed by a semi colon (;). Your script will fail without it. Also, notice that the email address for the $to variable is encased in double quotes. Double quotes tell PHP to read the string exactly as it is, without reading the special characters. If you encased the string in single quotes, you would have to escape the @ symbol like this: 'user\@email.com'.
-
-
3
Use PHP's mail function to send the email:
if(mail($to, 'YourWebSite.com: $subject',$body,"From: $addy\n"))
The mail function is structured in the following way: mail(recipient, subject, message, [headers, [parameters]]). For this example, the recipient is the $to variable, the subject is the $subject variable, the message is the $body variable and there are headers in the form of the $addy variable. There are no parameters.
This is the beginning of an "if" function. That is why there is not a semicolon at the end of the statement. The rest of the function is in the next step. -
4
Let the user know if the email was sent successfully:
{
echo 'Thank you, $name. Your email was sent successfully.';
}
else
{
echo "I'm sorry, there was a problem sending the email. Please check that you filled in all of the fields.";
}
?>The first part of this statement will print to the screen if the mail function works correctly. The second (the part after the "else") will print to the screen if there is a problem. "If" and "else" statements must be enclosed in curly braces {}.
-
5
Save the script as "emailme.php" and upload it to your server. It should look like this:
<?
$subject = $_POST['subject'];
$body = $_POST['body'];
$name = $_POST['name'];
$addy = $_POST['addy'];
$to = "user@email.com";if(mail($to, 'YourWebSite.com: $subject',$body,"From: $addy\n"))
{
echo 'Thank you, $name. Your email was sent successfully.';
}
else
{
echo "I'm sorry, there was a problem sending the email. Please check that you filled in all of the fields.";
}
?>
-
1
Tips & Warnings
There should be some validation included in any form you actually use on a web page. Check the resources for email validation scripts.