Processing Form Fields With PHP

How to Use PHP to Process and Handle Variables From Your Contact Form Fields

So now that we have our form set up, we need to process it and control what happens to the form data. The first step is to convert the variables of your form fields to something that is understood by php.

$email = $_POST[“emailaddress”];

This is converting the name of your text field “emailaddress” to the php equivalent “$email”. So what this will do is anytime you place “$email” in the file, it will produce whatever was entered into that form field. Now we need to add items to mail the results.

$to = “you@youremail.com”;
$subject = “New Email Address for Mailing List”;
$headers = “From: $email\n”;$message = “A visitor to your site has sent the following email address to be added to your mailing list.\nEmail Address: $email”;

Now you have indicated who will receive the form information. The subject line, and who its from. Notice how in the from area it calls “$email” … this will post the email address entered in the form.

We also added a small message, and the email address again.

The last step is to actually mail the information, and we do that using this line of code.

mail($to,$subject,$message,$headers);

So here is the full code for the “process.php” page to take the text field of your form, convert to php, and send the data to your email.

<?PHP
$email = $_POST[“emailaddress”];

$to = “you@youremail.com”;
$subject = “New Email Address for Mailing List”;
$headers = “From: $email\n”;

$message = “A visitor to your site has sent the following email address to be added to your mailing list.\n

Email Address: $email”;

mail($to,$subject,$message,$headers);

?>

 

Previous | Next

Using PHP To Send Contact Form Confirmation Emails

Send Your Contact Form Users a Message or Confirmation

By following the first 2 little articles you now have a simple form to collect an email address from your visitors, process the form with PHP and email you the data. But what if you want to send your visitor a confirmation email? The easiest way to do this is simply create another area of your code to send them the message like this.


$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you@youremailaddress.com\n";

$usermessage = "Thank you for subscribing to our mailing list.";

Then set the code to mail that information.

mail($user,$usersubject,$usermessage,$userheaders);

So here is the full code for the "process.php" page to take the text field of your form, convert to php, and send the data to your email AND send confirmation to your user.


<?PHP
$email = $_POST["emailaddress"];

$to = "you@youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";

$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n

Email Address: $email";

$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you@youremailaddress.com\n";

$usermessage = "Thank you for subscribing to our mailing list.";

mail($to,$subject,$message,$headers);

mail($user,$usersubject,$usermessage,$userheaders);

?>

 

Previous | Next

Creating a Contact Form Thank You Page and Redirect Using PHP

Send Your Users to a Custom Thank You Page Once Their Form Has Been Submitted

Because your form action is calling a separate page for processing rather then using itself (we will cover that later), you need to somehow let your visitor know that information has been sent.

To do this, you can pretty much use any html page with text as you normally would, and just place the php code at the top of the page. So for example, if you want to say something like “Thank You, Your information Has Been Submitted”. You would put your php at the top like this …

Click Here to have me build your custom contact form for you

Then the html below would look like this …

<html> <body> <table width="400" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <div align="center">Thank You, Your Information Has Been Submitted</div> </td> </tr> </table> </body> </html>


So here is the full code for the "process.php" page to show the thank you message when they submit their form. Keep in mind that the page still has to have a .php extension for your form to process correctly. It will still read the html just fine.


<?PHP
$email = $_POST["emailaddress"];

$to = "you@youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";

$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n

Email Address: $email";

$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you@youremailaddress.com\n";

$usermessage = "Thank you for subscribing to our mailing list.";

mail($to,$subject,$message,$headers);

mail($user,$usersubject,$usermessage,$userheaders);

?>


<html>
<body>
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div align="center">Thank You, Your Information Has Been Submitted</div>
</td>
</tr>
</table>
</body>
</html>


Or, if you prefer to redirect them to a new page and keep the process.php script completely separate and simply send them to a thank you page after the process has taken place, just put this redirect command after the mail function within your php script.


header("Location: thankyou.html");

Saving Form Field Data To Text and/or Excel Files With PHP

Take Any Contact Form Field and Store the Information to Text File or Excel Database

If you have been following along, by now you have a basic form that sends a visitors email address to you via email, and a conformation message to your visitor with a thank you message. Now lets say you wanted to store that email address into a text file on the server for use with another application, or backup.

The way this is done is by first creating a blank .txt file and uploading it to your server. Then you will need to give it permissions of 777 so the script can open and write to the file.

For your php script to open the text file you will need to place this into your code. In this example we assume you named your text file “email”.

$fh = fopen(“email.txt”, “a”);

The “a” you see is how the data will be entered into the file. In this example we are placing the new email address below the rest of them.

r = Opens the file for reading only and places the file pointer at the beginning of the file.

r+ = Opens the file for reading and writing and places the file pointer at the beginning of the file.

w = Opens the file for writing only and places the file pointer at the beginning of the file. If the file does not exist, it attempts to create it.

w+ = Opens the file for reading and writing and places the file pointer at the beginning of the file. If the file does not exist, it attempts to create it.

a = Opens the file for writing only and places the file pointer at the end of the file. If the file does not exist, it will try to create it (What we will use).

a+ = Opens the file for reading and writing and places the file pointer at the end of the file. If the file does not exist, it will attempt to create it.

Then the next step is to tell it what to write to the file. Note again the use of the “$email” to place the form data into the text file.

fwrite($fh, $email);

Then we need to close the file.

fclose($fh);

So your complete code looks like this.


<?PHP
$email = $_POST["emailaddress"];

$to = "you@youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";

$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n

Email Address: $email";

$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you@youremailaddress.com\n";

$usermessage = "Thank you for subscribing to our mailing list.";

mail($to,$subject,$message,$headers);

mail($user,$usersubject,$usermessage,$userheaders);

$fh = fopen("email.txt", "a");
fwrite($fh, $email);
fclose($fh);

?>


<html>
<body>
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div align="center">Thank You, Your Information Has Been Submitted</div>
</td>
</tr>
</table>
</body>
</html>

 


Previous | Next

Creating a Simple Send to Friend Form Script in PHP

Learn How to Build a Form To Allow Users to Send Your Page to a Friend

By now we should have the basics down, so I will introduce a new little tid bit for helping you create a send to friend script. We see these very often, and they can range in complexity, but this will give you that one key element that you will need (and probably having a hard time finding).

You know how to make a form and process it, but how do you capture the url of the page you are sending? You could just put the website in the form, but to send your friend the exact page is actually pretty simple.

$link = $_SERVER[‘HTTP_REFERER’];

That’s it. That will capture the url of the page your sending and process it as “$link”. From here you will make your form the way you normally would, and wherever you want the link to show in the sending email, place the “$link” and your all set.

Here is the sample html form to use.


<form action="process.php" method="post">
<table>
<tr>
<td>
Your Name<br>
<input name="yourname" type="text" size="40">
<br>
Friends Name<br>
<input name="friendsname" type="text" size="40">
<br>
Your Email Address<br>
<input name="youremailaddress" type="text" size="40">
<br>
Friends Email Address<br>
<input name="friendsemailaddress" type="text" size="40">
<br>
Message<br>
<textarea name="message" cols="30" rows= "5"wrap="PHYSICAL"></textarea>
<br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</td>
</tr>
</table>
</form>

Now we add the PHP to complete and process the send to friend script. We will use the same theory that we applied to our earlier examples, only this time there is more than just one form field, so we will need to process all of them.


<?PHP
$uname = $_POST["yourname"];
$fname = $_POST["friendsname"];
$uemail = $_POST["youremailaddress"];
$femail = $_POST["friendsemailaddress"];
$message = $_POST["message"];
$link = $_SERVER['HTTP_REFERER'];

$to = "$femail";
$subject = "Check out this site I found";
$headers = "From: $uemail\n";

$message = "Hi $fname.  Check out this site I found online today.
$link
$message";

mail($to,$subject,$message,$headers);

?>

That's it. Pretty easy stuff once you start getting the hang of it. It all works the same way, just adding more form fields to the process, but it can seem a little intimidating sometimes.

 

Previous

Home page

Welcome to PHP Contact Form

Simple, High Quality Contact Form Scripts For Every Day Solutions

In a pinch to get that project done? Tired of searching the internet and downloading endless form scripts looking for a “simple” solution? Well Look no further.

I’ve been in your shoes before, and decided to put together some practical, easy to use contact forms that will get you up and running in no time. Fully customizable and will allow for any design through simple css variables, and you can manipulate the contact forms for use with any fields, checkboxes, radio buttons and select options you need with fully documented code and secured to prevent header injection and spam – for only $9.95 each, or buy all 8 for only $29.95

Plus with our complete satisfaction guarantee – if you have any questions, or issues with your script, just me know and I will personally assist you with getting it on your website for you.

Contact Form Features at a Glance
  • Quickly collect user information and have it sent to your email, without exposing your email address to the public and email harvesting applications.
  • Anti spam protection against header injection and malicious attacks.
  • No programming skills required. One line of text to edit and your ready to go.
  • Complete CSS design styles to make for easy changes to the look and feel.
  • No database is needed.
  • Simple and easy to understand to make customizing a breeze.
  • Fully customizable for any web page or flash file.
  • Completely unbranded, with no reference or links back to our site.
  • Its yours to do with whatever you want. Unlimited usage on unlimited sites.
  • Free support if you have questions or need assistance.
What Others Have To Say

“WOW! I have tried countless php forms for my site … none have worked very well, or required WAY too much user-programming. This is awesome! It worked the first try! I Love it! Thank you so much!”

Andrea K – Boulder, CO


“You saved my project just in the nick of time. I had downloaded more then a dozen form scripts and tutorials and none of them made any sense until I found yours. Very well documented and I got my form finished in 5 minutes. Client was very happy. Thanks again”

Chad L – Houston, TX


“After I downloaded your form, I still didn’t understand. Maybe its just too complex for me, but I really appreciate your speedy response in helping get it set up on my website. It looks great and works just as I expected. Thank you for a great product and great service.”

Robert P – Macon, GA


Feel free to contact me here and share your thoughts and experience with PHP Contact Form.