Friday, August 5, 2011

HTML WebForm with PHP and MySQL - Part 2

Part 2 - Creating the PHP script (for email)

If you missed it; here is Part 1 - Creating the HTML form

"Prerequisites” – Your webhosting provider / server must support PHP scripts. I use Earthlink and they have a control center where I can enable/disable PHP scripts. Make sure to enable this prior to putting this live in production.

So far we have a HTML webform with the form tag method="post" that will gather the data for us. We also have the form tag action="sendresults.php" which we will use to send us the results to an email account.

NOTE: I can't take credit for this script as I stole it from here:

Whew……the boring stuff is out of the way, here are the steps to complete this:

Step 1: Download the file from this blog and save on your computer named "sendresults.php"

Step 2: Right click and edit this file in notepad.

Step 3: Change the following lines ($subject / $emailadd / $url)

Step 4: Save the file back to the server.

Let me take a minute to explain the $url. That "$" means that it is a variable and we can name it whatever we want. We can call it $bobisawesome if we wanted to. Basically this is being made we can use it later in the script without having to type in the long URL.

Now we can test the form. If you changed the field (variables) I told you to above, then you should be able to click submit on your form and in a few minutes, receive an email. You should also be redirected to another website (the $url variable) after clicking the submit button.

Let me break down some of the script for you. Look for this line close to the bottom:

mail($emailadd, $subject, $text, 'From: '.$emailadd.'');

This is actually the command that will email us the form. You can see I am building the email with the variables that we declared earlier in the script. Next look for this line:

echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

The ECHO command is the one that we will use to perform the next step (which in our case is to redirect to another page). If you look close at the end of the “ECHO” command, you will see our “$url” variable – yeah!!! The ECHO command can also be used to return something like: 1 row of data updated (which you will see more in the SQL part)


  • If you are redirected to a blank screen with the name of the PHP script in the address; that means that your script blew up. They don’t produce error codes due to security issues. Just know that you didn’t set something in the script properly.
  • Make sure that your email is not sitting in your junk email folder
  • Confirm that PHP scripts are enabled on your web server / hosting provider.
I have enclosed a copy of the PHP script on this blog. Stay tuned for our next part of the series: Part 3 – Putting the data into a SQL database. I know…..I can hardly wait myself.

No comments:

Post a Comment