Hi everyone
I’ve successfully created a contact form with php that gives the various required messages. :o Now I would like to add a simple random arithmetic captcha (non-image). See the anonymised (working) html form and existing (working, but without arithmetic captcha) php below.
The idea is to show “Incorrect answer” in the same way as the other error messages, or to pass to the Thankyou page for a correctly filled out form with correct answer. I’ve had a good go at this but can’t quite get it to work. Any assistance much appreciated.
Regards
Jumbo
HTML:
[code]
Contact Form
Required fields are in bold
Name:
Email address:
Telephone number:
Website (if applicable):
Area of interest: A B C
How did you hear about us? -- Please select -- Recommendation Internet Advertisement Other
Your message subject:
Your message:
Please answer the following arithmetic question: What is <?php echo $digit1;?> + <?php echo $digit2;?>?
[/code]PHP:
[php]<?php
/* Contact form with arithmetic captcha */
$myemail = "[email protected]";
/* Check all form inputs using check_input function */
$yourname = check_input($_POST[‘yourname’], “Enter your name”);
$email = check_input($_POST[‘email’]);
$telephone = check_input($_POST[‘telephone’]);
$website = check_input($_POST[‘website’]);
$likeit = check_input($_POST[‘likeit’]);
$how_find = check_input($_POST[‘how’]);
$subject = check_input($_POST[‘subject’], “Add a subject”);
$comments = check_input($_POST[‘comments’], “Add your message”);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w-]+@[\w-]+.[\w-]+)/", $email))
{
show_error(“Email address is not valid”);
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?://+[\w-]+.[\w-]+)/i", $website))
{
$website = ‘’;
}
/* Message for the email */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
Email: $email
Telephone: $telephone
URL: $website
Area of interest? $likeit
How did they find us? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thankyou page */
header(‘Location: thankyou.html’);
exit();
/* Functions used */
function check_input($data, $problem=’’)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div id="mainheader">
Error!
Please correct the following error:
<?php echo $myError; ?>
</div>
<div id="panel">
<div id="main" class="boxed">
<h2 class="heading">Main</h2>
<ul>
<li><a href="index.html">Home</a> </li>
<li><a href="about.html">About</a> </li>
<li><a href="contact.html">Contact</a> </li>
</ul>
</div>
<div id="services" class="boxed">
<h2 class="heading">Services</h2>
<ul>
<li><a href="services.html">Services</a> </li>
<li><a href="recent-projects.html">Recent projects</a> </li>
</ul>
</div>
<div id="pricing" class="boxed">
<h2 class="heading">Pricing</h2>
<ul>
<li><a href="pricing.html">Pricing</a> </li>
</ul>
</div>
<div id="info" class="boxed">
<h2 class="heading">Info</h2>
<ul>
<li><a href="tips-and-tricks.html">Tips and tricks</a> </li>
<li><a href="useful-links.html">Useful links</a> </li>
<li><a href="faq.html">Frequently asked questions</a> </li>
<li><a href="site-map.html">Site map</a> </li>
</ul>
</div>
<div id="contact" class="boxed">
<h2 class="heading">Contact</h2>
<ul>
<li><a href= "mailto:enquiries@X.co.uk">Contact by email</a> </li>
<li><strong>Telephone:<br />X</strong> </li>
</ul>
</div>
</div>
</body>
</html>
<?php
exit();
}
?>[/php]