top image
Email Forms 2
id: 5
Category: PHP
Rated: ---
Rate:



Well, we have a email form as in the first tutorial. However, this form can be subject to spam. How do we prevent this though?

It's far more simplier then most would probably think. It's not to say that this is the only way to accomplish this, but it's one of the most simple ways, especially if you only don't understand much about PHP.

Let's look at the main code:

<?PHP //send this Form as Email after submit.
if($_POST['send']){
     if( (empty($_POST['email']) OR empty($_POST['comments']) OR empty($_POST['name'])) && $_POST['send'] ){
    $error = "ERORR 1. - Massage dosen't sent!";     ?><br>

     <table width="100%" align="center" cellpadding="3" cellspacing="0" bgcolor="#FF3300">
     <tr>
      <td><div align="center"><strong>Please fill in all the fields that are marked with a *</strong></div></td>      </tr>
     </table>
<?      }else{

     // Email sent...........
     require($_SERVER['DOCUMENT_ROOT']."/scripts/class.phpmailer.php");
     $mail = new PHPMailer();

The above code is a small sample that has the code we need. Please note: First we are editing the php processing, and afterwards the HTML.

We are going to add a new input field in the code and make sure that people enter the same word or it wont email the form.

Code to edit:

if($_POST['send']){
     if( (empty($_POST['email']) OR empty($_POST['comments']) OR empty($_POST['name'])) && $_POST['send'] OR $_POST['special'] !== 'word'){
    $error = "ERORR 1. - Massage dosen't sent!";     ?><br>



What did I add and what does it do exactly?
OR $_POST['special'] !== 'word'
The above is saying this:
If any of the previous conditions are met or the input field with the name "special" doesn't have a string with that says "word" do no send the email. Keep in mind we are only editing part of the code! This is not the whole page.

Next we just need to add it in the HTML section of this page.

<form action = "#" method="post">
<table>
<tr>
<td>Your name*: </td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>Your E-Mail*</td>
<td>
<input type="text" name="email">
</td>
</tr>
<tr>
<td>
Enter the word "word" (no capitals): </td>
<td>
<input type="text" name="special">
</td>
</tr>
<tr>
<td>Comments*</td>
<td>
<textarea cols="30" rows="5" name="comments">Post your comments here. Thank you.</textarea>
</td>
</tr>
<tr>
<td><input type = "submit" name = "send" value = "Send Comments"></td>
<td><input type = "reset" name = "empty" value = "Empty the form"></td>
</tr>
</table>
</form>



Now we have added the field and name it special. With this and the php code in place, no one should be able to send an email without having the correct word in the "special" input text box. You can change it to being something other then word by replacing "word" with what you like. Same goes for the field named "special" this can be anything you want, just remember to change it in both the HTML and PHP.


Disclaimer

All contents are © Dice.
If credit for anything is missing please contact me and it will be added. If there is something you wish me to take off the site (that belongs to you) contact me. It will be removed immediatly.

Basic Rules

All tutorials on this site were written by me, Dice. They may not be redistributed without permission! Feel free to link to the videos.

I have used this site also for a project in college :3



home page

Contact Us

About Us