Jump to content


Photo

Adding an anti-spam question to PHP form


  • Please log in to reply
2 replies to this topic

#1 loulou28

loulou28

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 17 July 2013 - 11:42 AM

Hi there,

 

I only know a little php at this stage, and usually just find scripts and modify them as appropriate, however this is proving a little too difficult.

 

Basically I want to add an antispam question to a form which I've done in the HTML:

 

<form id="form" action="contact.php" method="post">
                 <table>
                    <tr>
                        <td align="right"><label for="name">Name:</label></td><td><input type="text" name="name" id="name" /></td>
                    </tr>
                    <tr>
                        <td align="right"><label for="email">Email:</label></td><td><input type="text" name="email" id="email"  /></td>
                    </tr>
                    <tr>
                        <td align="right"><label for="telephone">Telephone:</label></td><td><input name="telephone" type="text" id="telephone" /></td>
                    </tr>
                    <tr>
                        <td align="right"><label for="field">Anti-spam question:</label></td><td><input name="field" type="text" id="field" value="Complete the Beatles lyric: 'all you need is...'?"  onfocus="if(this.value==this.defaultValue) this.value='';"/></td>
                    </tr>
                    <tr>
                        <td align="right"><label for="message">Message:</label></td><td><textarea name="message"></textarea></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right"><img src="images/contactdots.gif" width="338" height="10" alt="" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right"><input id="button" type="Submit" value="SUBMIT" alt="submit" /></td>
                    </tr>
                </table>
               </form>

 

As you can see, the form is processed via contact.php, however I'm not sure how to validate the form anti-spam question.

I have originally just been using javascript for validation, however I realise this might need to change to PHP.

 

If anyone could advise of the correct code to validate this and then get it sent via contact.php which has the following code so far, that would be really appreciated...

 

<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){

// Pick up the form data and assign it to variables
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$tel = $_POST['telephone'];
$comments = stripslashes($_POST['message']);

// Build the email (replace the address in the $to section with your own)
$to = 'myemail@email.com';
$subject = "The Vintage Affair Web Quote enquiry";
$comments = "Name: $name \nEmail: $email \nTelephone: $tel \n\nDetails: $comments";
$headers = "From: myemail@email.com" . PHP_EOL . "Reply-To: myemail@email.com";

// Send the mail using PHPs mail() function
mail($to, $subject, $comments, $headers);

// Redirect
header("Location: thankyou.html");

}
?>
 



#2 fastsol

fastsol

    Advanced Member

  • Members
  • PipPipPip
  • 236 posts
  • LocationMinnesota

Posted 17 July 2013 - 11:54 AM

Yes you would want to validate this on php side, java side would be pointless and could easily get by.  Are you processing the form on the same page as the form?  Here is a example of how to work this.

if($_SERVER['REQUEST_METHOD'] == "POST"){

// Pick up the form data and assign it to variables
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$tel = $_POST['telephone'];
$comments = stripslashes($_POST['message']);
$field = strtolower($_POST['field']);
$spam_check = 'love';
if($field == $spam_check){
// Build the email (replace the address in the $to section with your own)
$to = 'myemail@email.com';
$subject = "The Vintage Affair Web Quote enquiry";
$comments = "Name: $name \nEmail: $email \nTelephone: $tel \n\nDetails: $comments";
$headers = "From: myemail@email.com" . PHP_EOL . "Reply-To: myemail@email.com";

// Send the mail using PHPs mail() function
mail($to, $subject, $comments, $headers);

// Redirect
header("Location: thankyou.html");
}
else{ echo 'Spam check failed!'; }
}

Also I do have a premade fully validated contact form that I distribute at http://amecms.com/ar...with-validation


You never know everything, I learn everday!

http://amecms.com

http://amewebdesigns.com


#3 loulou28

loulou28

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 18 July 2013 - 03:20 AM

That's worked a treat, thank you!

 

How would I go about adding that 'echo' just under the input that's been filled in incorrectly?

 

Thanks






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com