Making an HTML/PHP email form
Jeff Andersen (PHP / Moderate)If we would like to create a form that we can place on our site which people can use to send email to a specified address we have to start out by creating a new .PHP file (PHP is going to be used to tell the form what to do). We are going to be using the filename email.php.
We then have start PHP by including the <? and then we can start coding. We begin our processing code by starting an IF statement, this if statement (shown below) is saying IF submit is hit procede.
<?
if($_POST['submit'])
{
Then we tell the server that you would like it to email the address provided the specified variables. For this example we are going to use Subject, Email and Comments. Standard fields for a basic feedback form.
if($_POST['submit'])
{
mail("youremailaddress@whatever.com", "$subject", "$email", "$comments");
echo 'Sent';
We then close the if statement.
echo 'Sent';
}
We now use an ELSE statement telling the server that providing the IF statement is false, procede as well as we will close the PHP code so that we can insert the HTML portion of the email form.
else
{
?>
We can now start building our feedback form. We are going to keep this simple just to show you the basics of what has to be done to make a form work. We start off by initializing the form itself:
{
?>
<form method="post" action="email.php">
What this does is tells the browser that the following is a form, and will be used within the specified action and method. For this example we are telling it to post the information to the server to be then manipulated by email.php.
We now insert the fields that we would like to use for inputing information and sending.
<input name="subject" type="text" size="30">
<input name="email" type="text" size="30">
<textarea name="comments" rows="15" cols="100"></textarea>
<input name="submit" type="submit" value="Submit">
We have included two text fields and a text area along with the required submit button to intialize the IF statement when pressed. Now all that is left is to close the form tag, open the PHP code again so that we can end the else statement and close the PHP code to finish the document.
<input name="email" type="text" size="30">
<textarea name="comments" rows="15" cols="100"></textarea>
<input name="submit" type="submit" value="Submit">
</form>
<?
}
?>
This has been a basic usage of HTML forms, if you encounter a problem have a look at the full email.php code below to determine where you may have gone wrong.
<?
}
?>
<?
if($_POST['submit'])
{
mail("youremailaddress@whatever.com", "$subject", "$email", "$comments");
echo 'Sent';
}
else
{
?>
<form method="post" action="email.php">
<input name="subject" type="text" size="30">
<input name="email" type="text" size="30">
<textarea name="comments" rows="15" cols="100"></textarea>
<input name="submit" type="submit" value="Submit">
</form>
<?
}
?>
if($_POST['submit'])
{
mail("youremailaddress@whatever.com", "$subject", "$email", "$comments");
echo 'Sent';
}
else
{
?>
<form method="post" action="email.php">
<input name="subject" type="text" size="30">
<input name="email" type="text" size="30">
<textarea name="comments" rows="15" cols="100"></textarea>
<input name="submit" type="submit" value="Submit">
</form>
<?
}
?>
© Copyright 2006 - All Rights Reserved - SiteGuts is a part of the Enthonia Content Network