Jump to content

Ramjam

New Members
  • Posts

    5
  • Joined

  • Last visited

    Never

Everything posted by Ramjam

  1. Perfecto! Thanks again, xyph - job done.
  2. Hello again everyone. Update: The form is working nicely, and I've even had some incoming enquiries! The Session checks are doing their job well. A final tweak: I've currently got a few effective rules to sanitise the input and throw up appropriate error messages such as "Add your message title" or "Invalid email format", with the math captcha result message being generated independently. So the effect is: Once the form has been filled out correctly, the captcha result (either 'correct' or 'incorrect') is displayed as discussed. That's fine, but is there a simple way to make the captcha itself a rule so that it thows up an error message similarly to the other rules? Eg the email rule: /* Check all form inputs using check_input function */ ... $email = check_input($_POST['email']); ... /* If e-mail is not valid show error message */ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("Email address is not valid."); } ... /* 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)
  3. xyph, PFMaBiSmAd - Thanks to you both for the additional caveats. I'm on the case! All the best & keep up the good work.
  4. To xyph: Thank you - everything is working perfectly! I compliment you on your elegant and concise code; exactly what I was hoping for. Your clear explanation helped me to see where I had been going wrong & you have saved me a page of code; my own clumsy solution had 3 pages whereas yours only needs 2. I agree with your observation re non-image code, but many of my site visitors are unable to decipher visual captchas & I reckon this dynamic maths captcha will surely confound my former spammers. Mods - case closed! - please mark this one as Solved. Excellent forum. All the best.
  5. Hi everyone I've successfully created a contact form with php that gives the various required messages. 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. ___ HTML: <div id="form" class="form"> <h3 class="title">Contact Form</h3> <p>Required fields are in <strong>bold</strong></p> <form action="contact.php" method="post" target="_blank"> <p><strong>Name:</strong><br /><input type="text" name="yourname" size="35" /></p> <p><strong>Email address:</strong><br /><input type="text" name="email" size="35"/></p> <p>Telephone number:<br /><input type="text" name="telephone" size="35"/></p> <p>Website (if applicable):<br /><input type="text" name="website" size="35"/></p> <p>Area of interest: <input type="radio" name="likeit" value="A" checked="checked" /> A <input type="radio" name="likeit" value="B" /> B <input type="radio" name="likeit" value="C" /> C</p> <p>How did you hear about us? <select name="how"> <option value=""> -- Please select -- </option> <option>Recommendation</option> <option>Internet</option> <option>Advertisement</option> <option>Other</option> </select></p> <p><strong>Your message subject:</strong><br /><input type="text" name="subject" size="35"/></p> <p><strong>Your message:</strong><br /> <textarea name="comments" rows="10" cols="40"></textarea></p> <p>Please answer the following arithmetic question: What is <?php echo $digit1;?> + <?php echo $digit2;?>? <input name="captcha" type="text" size="2" id="captcha"/></p> <p><input type="submit" value="Send" /></p> </form> PHP: <?php /* Contact form with arithmetic captcha */ $myemail = "enquiries@X.co.uk"; /* 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) { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- Head data in here --> <html xmlns="http://www.w3.org/1999/xhtml"> <body> <div id="mainheader"> <div id="mainlogo"> <h1><a href="http://www.X.co.uk/" title="X"> <img style="border:0;width: 260px; height: 160px;" src="images/X.jpg" alt="X" /></a></h1> </div> </div> <div id="content"> <div class="content"> <h2 class="title">Error!</h2> <p><strong>Please correct the following error:</strong></p> <p><?php echo $myError; ?></p> </div> </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&#38;#64;X.co.uk">Contact by email</a> </li> <li><strong>Telephone:<br />X</strong> </li> </ul> </div> </div> <div id="mainfooter"> <p> &#38;#169; 2011 X<br />Designed by <a href="http://www.X.co.uk/" title="X"><strong>X</strong></a> </p> <a href="http://validator.w3.org/check?uri=referer" title="Valid XHTML 1.0"> <img style="border:0;width:88px;height:31px" src="images/valid-xhtml10.png" alt="Valid XHTML 1.0" /> </a> <a href="http://jigsaw.w3.org/css-validator/check/referer" title="Valid CSS!"> <img style="border:0;width:88px;height:31px" src="images/vcss.gif" alt="Valid CSS!" /> </a> </div> </body> </html> <?php exit(); } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.