Jump to content

Help with PHP form and anti spam question


helenS

Recommended Posts

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 //-->


    

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • cyberRobot changed the title to Help with PHP form and anti spam question

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.