helenS Posted January 13, 2020 Share Posted January 13, 2020 Below is my contact from - and I have set anti spam question as I don't like captcha. How to I code the post/human bit so it is case insensitive? I am a designer so I don't know what I am doing ☹️. <div class="one-half-column-right" id="contactform"> <form method="post" action="index.php#contactform"> <label>Name*</label> <div class="clear"></div> <input name="name" placeholder="Type Here"> <label>Email*</label> <div class="clear"></div> <input name="email" type="email" placeholder="Type Here"> <label>Message</label> <textarea name="message" placeholder="Type Here"></textarea> <label>*If today is Tuesday, what is tomorrow? <br> [lowercase answer please]<br> (Anti-spam)</label> <input name="human" placeholder="Type Here"> <input id="submit" name="submit" type="submit" value="Submit"> </form> <?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'From: Website Form'; $to = ‘name@name.com’; $subject = 'website form enquiry'; $human = $_POST['human']; $headers .= 'From: '.$from."\r\n". 'Reply-To: '.$from."\r\n" . 'X-Mailer: PHP/' . phpversion(); $body = "From: $name\n E-Mail: $email\n Message:\n $message"; if ($_POST['submit'] && $human == ‘wednesday’) { if (mail ($to, $subject, $body, $from)) { echo '<p style="font-family: Montserrat, Helvetica, Arial, sans-serif; font-weight: 600; text-align:center; font-size: 16px; color: #000; text-transform: uppercase; background-color: #FFD700"> Request has been sent. We will get back to within 48 hours!<br></p>'; } else { echo '<p style="font-family: Montserrat, Helvetica, Arial, sans-serif; font-weight: 600; text-align:center; font-size: 16px; color: #000; text-transform: uppercase; background-color: #FFD700"> Something went wrong, go back and try again!</p>'; } } else if ($_POST['submit'] && $human != '') { echo '<p style="font-family: Montserrat, Helvetica, Arial, sans-serif; font-weight: 600; text-align:center; font-size: 16px; color: #000; text-transform: uppercase; background-color: #FFD700"> You answered the anti-spam question incorrectly!</p>'; } ?> <!--// form //--> Quote Link to comment https://forums.phpfreaks.com/topic/309837-help-with-php-form-and-anti-spam-question/ Share on other sites More sharing options...
kicken Posted January 13, 2020 Share Posted January 13, 2020 Run their input through strtolower before comparing. You may also want to trim. Quote Link to comment https://forums.phpfreaks.com/topic/309837-help-with-php-form-and-anti-spam-question/#findComment-1573423 Share on other sites More sharing options...
helenS Posted January 13, 2020 Author Share Posted January 13, 2020 Thanks for the reply but as a newbie to web design - I don't know php so if you can let me know how to have the answer as case insensitive. I do not know PHP! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/309837-help-with-php-form-and-anti-spam-question/#findComment-1573460 Share on other sites More sharing options...
requinix Posted January 13, 2020 Share Posted January 13, 2020 3 minutes ago, helenS said: I do not know PHP! Time to learn! There's a bit in there where it takes the value entered by the user and compares it to the correct answer. Use strtolower (check the examples on the page kicken linked) to get a lowercased version of the user's answer, and compare that result to the correct answer. Quote Link to comment https://forums.phpfreaks.com/topic/309837-help-with-php-form-and-anti-spam-question/#findComment-1573462 Share on other sites More sharing options...
Psycho Posted January 14, 2020 Share Posted January 14, 2020 Here is the line you would need to modify if ($_POST['submit'] && $human == ‘wednesday’) { Quote Link to comment https://forums.phpfreaks.com/topic/309837-help-with-php-form-and-anti-spam-question/#findComment-1573480 Share on other sites More sharing options...
gizmola Posted January 14, 2020 Share Posted January 14, 2020 Well here is where the variable is assigned: $human = $_POST['human']; This code will have issues if it's submitted without the various form fields filled in. You don't check for that for any of the form variables. Probably, the code would be better if all the form assignments was moved inside the the if ($_POST['submit'] ... condition because it makes no sense to try and process the form and assign local variables to form variables, if it wasn't. And in fact all the form variable assignments will benefit from changing them to be trimmed. $human = trim($_POST['human']); Better yet, if you want to leave the variable assignments at the top of the script, check that the variable actually exists so you don't get a runtime error if it doesn't: $human = !empty($_POST['human']) ? trim($_POST['human']) : ''; Now you fix your condition: if ($_POST['submit'] && strtolower($human) == 'wednesday') { Note that I fixed the quotes around wednesday. Originally they were backtics, which have a special function in PHP. Read these pages: trim, strtolower Quote Link to comment https://forums.phpfreaks.com/topic/309837-help-with-php-form-and-anti-spam-question/#findComment-1573489 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.