• Blake

    (@alexanderblake)


    I have a beautiful built-in contact page in this theme I’m working with, but the spam emails I’m getting are getting ridiculous. I would like to add a verification question to the form, but I’m not sure how/what to code in that page.

    I would like a question like:

    Verify you are human by answering the following question:
    “On the color wheel, the color that begins with G is: _____________”
    or
    “Everyone knows the sky is the color: _____________”

    I want them to type in the answer. I DON’T want to go to a plugin because I don’t want to move away from the contact page layout that the theme uses.

    Any coders have an answer I can throw into the contact.php file?

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You would look for the code where the form checks if the fields are completed. Then create another IF statement with the new value. Then if all fields are complete and for example sky = blue then send. But I need to see the actual code that sends your form.

    Thread Starter Blake

    (@alexanderblake)

    <?php
    $your_email=trim($_POST['your_email']);
    $your_web_site_name=trim($_POST['your_web_site_name']);
    ?>
    
    <?php
    //If the form is submitted
    if(isset($_POST['name'])) {
    
    		//Check to make sure that the name field is not empty
    		if(trim($_POST['name']) === '') {
    			$hasError = true;
    		} else {
    			$name = trim($_POST['name']);
    		}
    
    		//Check to make sure sure that a valid email address is submitted
    		if(trim($_POST['email']) === '')  {
    			$hasError = true;
    		} else if (!preg_match('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$^', trim($_POST['email']))) {
    			$hasError = true;
    			$errorMessage = $_POST['text_4'];
    		} else {
    			$email = trim($_POST['email']);
    		}
    
    		//phone
    		if(isset($_POST['phone'])) $phone = trim($_POST['phone']);
    
    		//company name
    		if(isset($_POST['company_name'])) $company_name = trim($_POST['company_name']);
    
    		//company url
    		if(isset($_POST['company_url'])) $company_url = trim($_POST['company_url']);		
    
    		//Check to make sure comments were entered
    		if(trim($_POST['message']) === '') {
    			$hasError = true;
    		} else {
    			if(function_exists('stripslashes')) {
    				$comments = stripslashes(trim($_POST['message']));
    			} else {
    				$comments = trim($_POST['message']);
    			}
    		}
    
    		//If there is no error, send the email
    		if(!isset($hasError)) {
    
    			$emailTo = $your_email;
    			$subject = 'Contact Form Submission from '.$name;
    
    			//message body
    			$body  ="Name: $name \n\n";
    			$body .="Email: $email \n\n";
    			if(isset($phone)) $body .="Phone:$phone\n\n";
    			if(isset($company_name)) $body .="Company Name: $company_name\n\n";
    			if(isset($company_url)) $body .="Company Url: $company_url \n\n";
    			$body .="Message: $comments";
    
    			$headers = 'From: '.$your_web_site_name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
    
    			mail($emailTo, $subject, $body, $headers);
    			$emailSent = true;
    		}
    }
    ?>
    
    <?php if(isset($emailSent) == true) { ?>
    	<div class="ok_box">
    		<h3><?php echo $_POST['text_1'];?>, <?php echo $name;?></h3>
    		<p><?php echo $_POST['text_2'];?></p>
    	</div>
    <?php } ?>
    
    <?php if(isset($hasError) ) { ?>
    	<div class="error_box">
    		<?php echo $_POST['text_3'];?>
    
    		<?php echo $errorMessage;?>
    	</div>
    <?php } ?>

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    Hopefully this helps!!

    I have been searching around for a while now, and I’d love to have this resolved so I can move forward. Once I can have an easy human verification field, I can then plug it into the other 18 sites on their network.

    Thanks!

    Create a text box on your form and label it “question” or whatever you choose. And put the correct answer where it says ‘answer here’. Try this after the check for the email.

    //Check to make sure sure verification question is answered and correct
    		if(trim($_POST['question']) === '')  {
    			$hasError = true;
    		} else if (trim($_POST['question']) != 'answer here') {
    			$hasError = true;
    
    		} else {
    			$email = trim($_POST['question']);
    		}
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add Dynamic/Verification Question to contact.php page’ is closed to new replies.