Email Forms in PHP - (2/4) | WebReference

Email Forms in PHP - (2/4)

To page 1current pageTo page 3To page 4
[previous][next]

Email Forms in PHP

The Form

Like any email form, the original form itself is quite simple. No great shakes here, just a standard HTML form with a POST method and the action pointing to the PHP page that will process the form results.

<form name="form" method="post" action="contact_thanks.php">
<p class="bodymd">Your Name<br>
<input type="text" name="Name">
</p>
<p class="bodymd">Your Email<br>
<input type="text" name="Email">
</p>
<p class="bodymd">Comments or Questions<br>
<textarea name="Comments" rows="5" cols="40"></textarea>
</p>
<p class="bodymd">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Form">
</p>
</form>

This form can be .html, .shtml or whatever you choose; the page that the results are handed to, however, must be a PHP page.

Processing the Mail

Take a look at the code on the next page. It is very typical of any scripting language, server-side or client-side, so if you have any amount of knowledge in JavaScript, ASP, JSP, Perl or the like, it should seem very familiar.

The code is a combination of validation, and, if validated, the email processor as well. Take a look at the first if statement, it is checking to see if any of the fields are empty. If any one field is, it then opens a form and prints a statement to inform the user that they missed some required field(s).

The statements after that are also simple. "If" statements check each of the individual fields to see if they are empty; if they are empty, the script prints that field again to be filled in (note that the form was already opened above if a field was empty). If it is not empty it then stores the value of that field in a hidden field.

To page 1current pageTo page 3To page 4
[previous][next]


Created: July 30, 2001
Revised: July 30, 2001

URL: https://webreference.com/programming/javascript/phpemail/2.html