HTML Forms & PHP

A HTML form is able to capture information; PHP takes this information posted by the HTML form and sends it via email. PHP is a common way to validate forms. Test it out by sending yourself an email.

Step 1:

You will need to create a basic HTML form with four items: two input tags, a textarea, and a submit input button. One input tag will be for the users name; the other will be for the users email. The textarea will be for a short message.

Step 2:

Create a file and save it as 'contact.php'

Contents of contact.php:

The 'if' statement below checks to make sure all required fields are not blank:

              if(count($_POST) > 0) {

HTML5 DOCTYPE, head and title tag:

              <!DOCTYPE html>
              <meta charset="UTF-8" />
              <title>PHP Contact Form</title>

Complete message appears when a valid form is submitted:

              <h2>Contact Form</h2>
              <?php echo($complete); ?>

htmlentities blocks extra code if it’s added to URL:

              <form action="<?php echo(htmlentities($_SERVER[PHP_SELF], ENT_QUOTES)); ?>" method="post">

The name attribute for the input and words in-between the $_POST[] brackets need to be the same:

              <label for="usr_name">Name</label>
              <input type="text" name="usr_name" value="<?php echo($_POST[usr_name]); ?>">
              <?php echo($message); ?> <br />

The PHP inside the value attribute is what maintains input value

              <label for="usr_emil">Email</label>
              <input type="text" name="usr_email" value="<?php echo($_POST[usr_email]); ?>">
              <?php echo($e_message); ?><br />

PHP must be put in-between textarea opening and closing tags to maintains values:

	          <label for="message">Message</label>
              <textarea name="message">
              <?php echo($_POST[message]); ?>
              </textarea><br />
              <input type="submit" value="Send Email">
              <br />

Step 3:

Create a second file and save it as ‘process.php’, both of these files need to be in the same folder.

Contents of process.php:

Resets messages and errors:

              $message = "";
              $e_message = "";

              $valid = true;

This function checks if the user has entered a valid email:

              function is_valid_email($email = NULL){$email = (string) $email;if(!is_null($email)){if(extension_loaded('filter')){return((boolean) filter_var($email, FILTER_VALIDATE_EMAIL));}if(preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $email)){/** This function is only available on Windows platforms as of PHP 5.3.0 **/if(!function_exists('checkdnsrr')){return(true);}else{if(checkdnsrr(array_pop(explode('@',$email)), 'ANY')){return(true);}}}}return(false);}

Uses the above function, returns error if invalid email is found

              if( !is_valid_email( $_POST[usr_email] ) ) {
                  $e_message .= "Please enter a valid email.";
                  $valid = false;

Checks if either name or email input is empty, returns errors if so:

              if( empty($_POST[usr_name]) || empty($_POST[usr_email]) ){
                  $message .= "This field cannot be blank.";
                  $valid = false;

Helps protect against spamming

              $safe_email = str_replace("\r\n", '',$_POST[usr_email]);

If no errors are found, everything in the "{" "}" is done:

              if( $valid ) {
                  $body = "Form Information\n";
                  $body .= "Name: $_POST[usr_name]\n";
                  $body .= "Email: $_POST[usr_email]\n";
                  $body .= "Message: $_POST[message]\n";
                  $subject = "PHP Contract Form";
                  $header = "From: $safe_email\r\n";
                  $header .= "Return-Path:\r\n";
                  $sendTo = "";
                  mail($sendTo, $subject, $body, $header);
                  $complete = "<h3>Thanks for your information.</h3>\n";

Step 4:

Change the email "" (make sure the quotes stay around your email) on both line 23 and line 24 to the email address you want the information sent to.

Step 5:

Nearly every hosting company has PHP enabled. PHP only works when uploaded via FTP to your web server, not locally.

Step 6:

Once uploaded to your server, navigate to where you've uploaded "contact.php" and test your HTML form. Finally, check the email you used in ‘process.php’ to make sure the email has sent.

Downloads Files