Hey everyone, so my work wants me to update their website with a email contact form, i foolishly agreed not realising what i was getting myself into, i didn't know how complicated php is (i am an artist, not a coder :icon_redface: ), it's taken me about a week and a half so far to get to this point, i've read a few books, checked out some tutorials online and cobbled together a rough version on my own site.

I'm quite proud that i've made it so far but now i am stuck, i wanted to include a captcha so as to help keep out spam but it just doesn't work, so long as you enter an email into the email field you can submit the form and it'll end up in my inbox, this is frustrating and i really don't know what to do at this point. Can anyone here look at this and see what mistake(s) i've made?

Here is the one i've put up onto my own site if you're interested:
http://www.bboykrillin.com/contact.php

Here is the code on the contact.php page:

<?php 
						if ($_POST["email"]<>'') { 
							$ToEmail = 'email@website.com'; 
							$EmailSubject = 'Contact about... '; 
							$mailheader = "From: ".$_POST["email"]."\r\n"; 
							$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
							$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
							$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
							$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
							$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
							mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
						?> 
					<p>Your message was sent</p>

						<?php 
						} else { 
						?> 
                     
					<form action="contact.php" method="post">
					<p>Your name:</p>
                    <input name="name" type="text" id="name" size="32"><br />
					<p>Email address:</p>
                    <input name="email" type="text" id="email" size="32"><br />
					<p>Comment:</p>
                    <textarea name="comment" cols="45" rows="15" id="comment" class="bodytext"></textarea>
					

					<?php
 
					// call the lib..
					require_once('recaptchalib.php');
 
					// Get a key from http://recaptcha.net/api/getkey
					$publickey = "xXxXxXxXxXxXxXxXx";
					$privatekey = "xXxXxXxXxXxXxXxXx";
 
					# was there a reCAPTCHA response?
					if ($_POST["submit"]) {
					    $response = recaptcha_check_answer($privatekey,
        					$_SERVER["REMOTE_ADDR"],
        					$_POST["recaptcha_challenge_field"],
        					$_POST["recaptcha_response_field"]);
 
        					if ($response->is_valid) {
                			echo "Yes, that was correct!";
        					} else {
                	# set the error code so that we can display it
        	echo "Eh, That wasn't right. Try Again.";
 
        	}
			}
			?>
 
			<form action="contact.php" method="post">
			<?php echo recaptcha_get_html($publickey, $error); ?>
			<input style="width: 317px" type="submit" value="submit" name="submit"/>
			</form>

 
			<?php 
			}; 
			?>

And here is the code for the verify.php page:

<?php
  require_once('recaptchalib.php');
  $privatekey = "xXxXxXxXxXxXxXxXx";
  $response = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } else {
    // Your code here to handle a successful verification
  }
  ?>

I also used the recaptchalib.php code downloaded from www.google.com/recaptcha which i have not modified at all, and the tutorial that gave me the most infomation is at: http://vidiame.com/php/how-to-implement-recaptcha-with-your-php-project (don't know if that's any good to you guys, just trying to give as much info as possible!)

Thanks for taking a look, i hope someone here can see what it is i am missing, i am stumped, the problems with being an artist and not a coder i guess :confused:

Recommended Answers

All 4 Replies

I have tested it to work as below. Just put your key.
It works in webs server, it may not work in local machine.

<html>
<body>

<form action="recap.php" method="post">
<?php
// Get a key from https://www.google.com/recaptcha/admin/create
$publickey = "";
$privatekey = "";

include('recaptchalib.php');

# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;

# was there a reCAPTCHA response?
if ($_POST["recaptcha_response_field"]) {
        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["recaptcha_challenge_field"],
                                       $_POST["recaptcha_response_field"]);

        if ($resp->is_valid) {
                echo "You got it!";

$ToEmail = 'prakash@sourcebits.com';

$EmailSubject = 'Contact about... ';

$mailheader = "From: ".$_POST["email"]."\r\n";

$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";

$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";

$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";

$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";

$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>";

mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
        } else {
                # set the error code so that we can display it
                $error = $resp->error;
        }
}
?>
<br/>
<p>Your name:</p>

<input name="name" type="text" id="name" size="32"><br />

<p>Email address:</p>

<input name="email" type="text" id="email" size="32"><br />

<p>Comment:</p>

<textarea name="comment" cols="45" rows="15" id="comment" class="bodytext"></textarea>
<p>
     <?php echo recaptcha_get_html($publickey,$error); ?>
    <input type="submit" value="submit"/>
    </form>
  </body>
</html>
commented: Clear and to the point response, was very helpful. +5

Ah! thank you very much for replying Sourcebits, I haven't had a chance to check back on this thread recently, the joys of late night shift work! ^^

I am guessing recap.php is a renamed version of the contact.php file? Or is it something new entirly? Sorry for being a little slow, I'll leave the programming to the programmers in future :D

recap.php is your contact form in that example.
I'd suggest going to w3schools to learn php

Cheers Metalix, i had already guessed that though, the form is working just fine now ^^

Yeah, i'm not so much interested in the programming side of web design, more the visual side really, i'm an artist and as you can see i don't really have the skills for .php. It would be helpful to know more but as of right now i have enough on my plate with all my other projects!

If only there were more hours in the day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.