The only difference being that the action will point to email-data.php (this file). Save form.html when you are done. Alternatively, you could copy form.html and name it something else (for example, form-test-email.html) and then change the action value in form-test-email.html to email-data.php. That would allow you to have form.html for testing the form with show-data.php and form-test-email.html for testing the form with email-form.php. 4) After that, place the form HTML file, the css folder, and this file (email-data.php) on a web server that has PHP installed. For example, if you have an account with a web host, you could upload the files to your server (see Chapter 21). The vast majority of web host accounts have PHP pre-installed, so you shouldn't have to do anything special beyond uploading the files. But, if the form doesn't work after you upload the files, your web host or their site's help pages may be of assistance. You may also test the form on your own computer if you have PHP and a web server such as Apache installed and running. OS X (Macs) has these installed by default. More information for enabling these is available at, although some of the process is a little technical. You may find it easier to download one of the following packages that install Apache, PHP, MySQL and more on your computer, and provide an interface for you to turn them on and off easily. - OS X only: MAMP ( - Windows only: WampServer ( - Linux, OS, or Windows: XAMPP ( Search online for more information about using one of these packages. IMPORTANT: However, unlike running show-data.php on your computer's server, email-data.php may require some additional configuring of your computer so PHP's mail() function will send the email. Search online for additional information if email-data.php is not sending emails when run from your machine. Extra configuration probably won't be necessary if you run this on your web host's server. */ if (empty($_POST)) { print "

No data was submitted.

"; exit(); } /* Creates function that removes magic escaping, if it's been applied, from values and then removes extra newlines and returns to foil spammers. Thanks Larry Ullman! */ function clear_user_input($value) { if (get_magic_quotes_gpc()) $value=stripslashes($value); $value= str_replace( "\n", '', trim($value)); $value= str_replace( "\r", '', $value); return $value; } /* Create body of email message by cleaning each field and then appending each name and value to it. */ $body = "Here is the data that was submitted:\n"; // Get value for each form field foreach ($_POST as $key => $value) { // True if for field is anything but one of the Email sign-up checkboxes if ($key != 'email_signup') { $key = clear_user_input($key); $value = clear_user_input($value); $body .= "$key: $value\n"; } else { // True if an Email checkbox chosen if (is_array($_POST['email_signup'])) { $body .= "$key: "; $counter =1; foreach ($_POST['email_signup'] as $value) { //Add comma and space until last element if (sizeof($_POST['email_signup']) == $counter) { $body .= "$value\n"; break; } else { $body .= "$value, "; $counter += 1; } } // end foreach } // end inner if } // end else } // end foreach extract($_POST); /* Get file upload picture name */ if(isset($_FILES['picture'])) { /* This basic script presents the name of the uploaded file only. To check the file size, file type (like JPG, GIF, or PNG), and actually upload a file to a folder, see the video tutorial at The code explained in the video is also available for download from that URL. That page also includes links to a series of videos about using PHP. */ $picture_name = $_FILES['picture']['name']; // make sure name isn't blank if ($picture_name != '') { // add the picture name to the email body message $body .= "picture: $picture_name\n"; } } /* Removes newlines and returns from $email and $name so they can't smuggle extra email addresses for spammers */ $email = clear_user_input($email); $first_name = clear_user_input($first_name); /* Create header that puts email in From box along with name in parentheses and sends Bcc to alternate address. Change to the Bcc email address you want to include. */ $from='From: '. $email . "(" . $first_name . ")" . "\r\n" . 'Bcc:' . "\r\n"; // Creates intelligible subject line that also shows you where it came from $subject = 'New Profile from Website'; /* Sends mail to the address below with the form data submitted above. Replace with the email address to which you want the data sent. */ mail('', $subject, $body, $from); // This message will appear in the browser, not as part of the email print "

Thanks for signing up! Your information has been sent to us.

"; ?>