PeggyMD53 Posted March 12, 2009 Share Posted March 12, 2009 I am a neophite to php. I have a form that I need to ad reCaptcha to and when successful redirect to a Thank You page. If I replace the header line below with an echo message, that works. If I put the header line back in the code, I get the header error message. What I would like to do when the reCaptcha is successful is to redirect visitors to an already created Thank you page, and not just a Thank you message that appears below the reCaptcha. I have read your HEADER ERRORS - READ HERE BEFORE POSTING THEM several times about putting the php in the header of the html page, but still not sure how to actually do this since I really don't know php. If I try to insert the code in the if valid area with the header, I of course get the header error you talk about here. "Warning: Cannot modify header information - headers already sent by (output started at /home/saturn/public_html/newsform4.php:10) in /home/mywebsite/public_html/form.php on line 55" I have spent several days getting to this point and have tried several things, but nothing has worked. Thank you in advance for your help. Peggy Dixon Here is the form page code with the reCaptcha in itRe... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>News Form</title> </head> <body> <form method="post" action=""> <label for="firstname">First Name:</label> <input type="text" name="firstname" id="firstname" /><br /> <label for="lastname"><strong>Last Name:</strong></label> <input type="text" name="lastname" id="lastname" /><br /> <label for="orgname"><strong>Organization Name:</strong></label> <input type="text" name="orgname" id="orgname" /><br /> <label for="address1"><strong>Address 1:</strong></label> <input type="text" name="address1" id="address1" /><br /> <!--put the following script and noscript in the spot where you want the recaptcha to appear and this puts the captcha where you want it to appear on your page --> <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=<public code from the reCaptcha site >"> </script> <noscript> <iframe src="http://api.recaptcha.net/noscript?k=<private code from the reCaptcha site >" width="350" frameborder="0"></iframe><br> <textarea name="recaptcha_challenge_field" rows="3" cols="40"> </textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"> </noscript> <?php require_once('recaptchalib.php'); // Get a key from http://recaptcha.net/api/getkey $publickey = "public code from the reCaptcha site "; $privatekey = "private code from the reCaptcha site "; # 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) { $firstname = $_REQUEST['firstname'] ; $lastname = $_REQUEST['lastname'] ; $orgname = $_REQUEST['orgname'] ; $address1 = $_REQUEST['address1'] ; mail( "info@mywebsite.com", "Feedback Form Results", "Organization: $orgname \nAddress: $address1", "From: $firstname $lastname <$email>"); header ( "Location: http://www.mywebsite.com/thanks.html"); } else {# set the error code so that we can display it $error = $resp->error; } } echo recaptcha_get_html($publickey, $error); ?><br /> <label><input name="submit" type="submit" id="submit" value="Submit" /></label> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/149029-solved-if-recaptcha-is-valid-get-header-error/ Share on other sites More sharing options...
MadTechie Posted March 12, 2009 Share Posted March 12, 2009 Okay see all that HTML at the start of the page! thats not allowed try this <?php require_once('recaptchalib.php'); // Get a key from http://recaptcha.net/api/getkey $publickey = "public code from the reCaptcha site "; $privatekey = "private code from the reCaptcha site "; # 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) { $firstname = $_REQUEST['firstname'] ; $lastname = $_REQUEST['lastname'] ; $orgname = $_REQUEST['orgname'] ; $address1 = $_REQUEST['address1'] ; mail( "info@mywebsite.com", "Feedback Form Results", "Organization: $orgname \nAddress: $address1", "From: $firstname $lastname <$email>"); header ( "Location: http://www.mywebsite.com/thanks.html"); } else {# set the error code so that we can display it $error = $resp->error; } } $reCAP = recaptcha_get_html($publickey, $error); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>News Form</title> </head> <body> <form method="post" action=""> <label for="firstname">First Name:</label> <input type="text" name="firstname" id="firstname" /><br /> <label for="lastname"><strong>Last Name:</strong></label> <input type="text" name="lastname" id="lastname" /><br /> <label for="orgname"><strong>Organization Name:</strong></label> <input type="text" name="orgname" id="orgname" /><br /> <label for="address1"><strong>Address 1:</strong></label> <input type="text" name="address1" id="address1" /><br /> <!--put the following script and noscript in the spot where you want the recaptcha to appear and this puts the captcha where you want it to appear on your page --> <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=<public code from the reCaptcha site >"> </script> <noscript> <iframe src="http://api.recaptcha.net/noscript?k=<private code from the reCaptcha site >" width="350" frameborder="0"></iframe><br> <textarea name="recaptcha_challenge_field" rows="3" cols="40"> </textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"> </noscript> <?php echo $reCAP;?> <br /> <label><input name="submit" type="submit" id="submit" value="Submit" /></label> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/149029-solved-if-recaptcha-is-valid-get-header-error/#findComment-782580 Share on other sites More sharing options...
PeggyMD53 Posted March 12, 2009 Author Share Posted March 12, 2009 To MadTechie, Thank you soooooooo much. Your fix worked the first time. I really appreciate the help. PeggyMD53 Quote Link to comment https://forums.phpfreaks.com/topic/149029-solved-if-recaptcha-is-valid-get-header-error/#findComment-783360 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.