Jump to content

PHP Form working but not with recaptcha


paul1
 Share

Recommended Posts

Please can sombody help me i have a PHP form and it is fine but i am trying to add recaptcha to the form. The form is already validated and i have having trouble with were the recaptcha is to be placed for it to work. I don't know much PHP but i am good a following instructions or advise. From what i know the form validates in PHP and then javascript, but i dont know how to add a instruction to validate the form then stop and hold the information until the recaptcha processes is finish then send post the form.Any help i would be so happy with, i have been playing with this for days now. I will post the code for the form and recaptcha not the mess i have been making. <?php // php for recaptcharequire_once('recaptchalib.php'); // reCAPTCHA Library$pubkey = ""; // Public API Key$privkey = ""; // Private API Key if ($_POST['doVerify']) { $verify = recaptcha_check_answer($privkey, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']); if ($verify->is_valid) { # Enter Success Code echo "Your response was correct!"; } else { # Enter Failure Code echo "You did not enter the correct words. Please try again."; }}?>-------------------------------------------------------------------------------------------------------------------------------------------------------------------<div> <!--the form information from recaptcha that i have--><form method="post" action="verify.php"><?php echo recaptcha_get_html($pubkey, $verify->error); ?><input type="submit" name="doVerify" value="Verify" /></form></div>-------------------------------------------------------------------------------------------------------------------------------------------------------------------<?php // Start of my form php data// Set email variables$email_to = 'My email Address inputting';$email_subject = 'Form submission';// Set required fields$required_fields = array('fullname','email','comment');// set error messages$error_messages = array( 'fullname' => 'Please enter a Name to proceed.', 'email' => 'Please enter a valid Email Address to continue.', 'comment' => 'Please enter your Message to continue.');// Set form status$form_complete = FALSE;// configure validation array$validation = array();// check form submittalif(!empty($_POST)) { // Sanitise POST array foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value)); // Loop into required fields and make sure they match our needs foreach($required_fields as $field) { // the field has been submitted? if(!array_key_exists($field, $_POST)) array_push($validation, $field); // check there is information in the field? if($_POST[$field] == '') array_push($validation, $field); // validate the email address supplied if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field); } // basic validation result if(count($validation) == 0) { // Prepare our content string $email_content = 'New Website Comment: ' . "\n\n"; // simple email content foreach($_POST as $key => $value) { if($key != 'submit') $email_content .= $key . ': ' . $value . "\n"; } // if validation passed ok then send the email mail($email_to, $email_subject, $email_content); // Update form switch $form_complete = TRUE; }}function validate_email_address($email = FALSE) { return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;}function remove_email_injection($field = FALSE) { return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));}?>------------------------------------------------------------------------------------------------------------------------------------------------------------------<div id="formWrap"><!--My form information--> <h2>We Appreciate Your Feedback</h2><div id="form"><?php if($form_complete === FALSE): ?><form action="contact.php" method="post" id="comments_form"> <div class="row"> <div class="label">Your Name</div> <!-- end .label --> <div class="input"> <input type="text" id="fullname" class="detail" name="fullname" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>" /><?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?> </div><!-- end .input --> <div class="context">e.g. John Smith or Jane Doe</div><!-- end .context --> </div><!-- end .row --> <div class="row"> <div class="label">Your Email Address</div> <!-- end .label --> <div class="input"> <input type="text" id="email" class="detail" name="email" value=<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>"" /><?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?> </div><!-- end .input --> <div class="context">We will not share your email with anyone.</div><!-- end .context --> </div><!-- end .row --> <div class="row"> <div class="label">Your Message</div> <!-- end .label --> <div class="input2"> <textarea id="comment" name="comment" class="mess"><?php echo isset($_POST['comment'])? $_POST['comment'] : ''; ?></textarea><?php if(in_array('comment', $validation)): ?><span class="error"><?php echo $error_messages['comment']; ?></span><?php endif; ?> </div><!-- end .input --> </div><!-- end .row --> <div class="submit"> <input type="submit" id="submit" name="submit" value="Send Message" /> </div><!-- end .submit --> </form> <?php else: ?><p style="font-size:35px; font-family:Arial; color:#255E67; margin-left:25px;">Thank you for your Message!</p><script type="text/javascript">setTimeout('ourRedirect()', 5000)function ourRedirect(){ location.href='index.html'}</script><?php endif; ?></div><!-- end #form --></div><!-- end formWrap --> Thanks if anybody can help me.

Link to comment
Share on other sites

you want to chek dynamically captcha first then submit the form. right? you need to use ajax to submit the captcha to php page which will validate the capctha if it matches you can then send the rest of the data of the form. unless you want to do it dynamically you can send all the data with captcha in php page and process it there.

Link to comment
Share on other sites

What would be the best why of doing this with ajax or posting the whole form? With ajax will it hold the details of the for until the recaptcha has been validated, and also with ajax will i need a new PHP file to hold the recaptcha PHP verify file.Thank you.

Link to comment
Share on other sites

both will work same as functionalty. with ajax it will increase user experiance.ajax is also javascript which makes request to php file and get response from it.there you can start http://w3schools.com/ajax

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...