kcelsi Posted January 6, 2014 Share Posted January 6, 2014 Hello, I have some basic php knowledge and have a page set up to use a Captcha in a form. Using this code that I obtained, everything works fine, but after the form is submitted, instead of the page displaying text on the same page that says "Email Sent", I want a new page (which I have already created) to come up that displays a Thank You message. Thanks for any help! Here is the code: <?php require_once('recaptchalib.php'); // Get a key from http://recaptcha.net/api/getkey $publickey = "6Ldke-wSAAAAAE7yRt4cUb3G6hYU__6JIwsc6lwL"; $privatekey = "6Ldke-wSAAAAAPrYCfs4gR28Y10hEttom0ax-nKD"; # 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) { $company = $_POST['company']; $fullname = $_POST['fullname']; $address = $_POST['address']; $address2 = $_POST['address2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $phone = $_POST['phone']; $fax = $_POST['fax']; $email = $_POST['email']; $category = $_POST['category']; $message = $_POST['message']; $subject = "Marketing Inquiry"; $company = stripslashes($company); $fullname = stripslashes($fullname); $address = stripslashes($address); $city = stripslashes($city); $state = stripslashes($state); $zip = stripslashes($zip); $phone = stripslashes($phone); $fax = stripslashes($fax); $email = stripslashes($email); $category = stripslashes($category); $message = stripslashes($message); $subject = stripslashes($subject); $final_message = "You have a marketing inquiry\n\n"; $final_message .= "From: " . $fullname . "\n"; $final_message .= "Address: " . $address . "\n"; $final_message .= "Address 2: " . $address2 . "\n"; $final_message .= "City: " . $city . "\n"; $final_message .= "State: " . $state . "\n"; $final_message .= "Zip Code: " . $zip . "\n"; $final_message .= "Phone: " . $phone . "\n"; $final_message .= "Fax: " . $fax . "\n"; $final_message .= "Email: " . $email . "\n"; $final_message .= "Category: " . $category . "\n"; $final_message .= "Message: " . $message . "\n"; mail("email@samplepage.com", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$final_message, "From: ".$fullname); echo "Email sent"; } else { # set the error code so that we can display it $error = $resp->error; echo $error; } } echo recaptcha_get_html($publickey, $error); ?> Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/ Share on other sites More sharing options...
TinyI Posted January 6, 2014 Share Posted January 6, 2014 I would use exit(header(location:http://pageurlgoeshere)); Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464081 Share on other sites More sharing options...
kcelsi Posted January 6, 2014 Author Share Posted January 6, 2014 I tried this and the page gives me this syntax error: Parse error: syntax error, unexpected ':' in /home/ideal/public_html/Contact_Marketing.php on line 312 <?php require_once('recaptchalib.php'); // Get a key from http://recaptcha.net/api/getkey $publickey = "6Ldke-wSAAAAAE7yRt4cUb3G6hYU__6JIwsc6lwL"; $privatekey = "6Ldke-wSAAAAAPrYCfs4gR28Y10hEttom0ax-nKD"; # 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) { $company = $_POST['company']; $fullname = $_POST['fullname']; $address = $_POST['address']; $address2 = $_POST['address2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $phone = $_POST['phone']; $fax = $_POST['fax']; $email = $_POST['email']; $category = $_POST['category']; $message = $_POST['message']; $subject = "Marketing Inquiry"; $company = stripslashes($company); $fullname = stripslashes($fullname); $address = stripslashes($address); $city = stripslashes($city); $state = stripslashes($state); $zip = stripslashes($zip); $phone = stripslashes($phone); $fax = stripslashes($fax); $email = stripslashes($email); $category = stripslashes($category); $message = stripslashes($message); $subject = stripslashes($subject); $final_message = "You have a marketing inquiry\n\n"; $final_message .= "From: " . $fullname . "\n"; $final_message .= "Address: " . $address . "\n"; $final_message .= "Address 2: " . $address2 . "\n"; $final_message .= "City: " . $city . "\n"; $final_message .= "State: " . $state . "\n"; $final_message .= "Zip Code: " . $zip . "\n"; $final_message .= "Phone: " . $phone . "\n"; $final_message .= "Fax: " . $fax . "\n"; $final_message .= "Email: " . $email . "\n"; $final_message .= "Category: " . $category . "\n"; $final_message .= "Message: " . $message . "\n"; mail("graphics_Beth@idealwindow.com", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$final_message, "From: ".$fullname); exit(header(location:http://www.idealwindow.com/Thank_You.html)); } else { # set the error code so that we can display it $error = $resp->error; echo $error; } } echo recaptcha_get_html($publickey, $error); ?> Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464091 Share on other sites More sharing options...
TinyI Posted January 6, 2014 Share Posted January 6, 2014 my bad! forgot the quotes! exit(header('location:http://www.google.com')); that should work. I've tested that just now on some code. Regards, TinyI Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464093 Share on other sites More sharing options...
kcelsi Posted January 6, 2014 Author Share Posted January 6, 2014 Thanks. Now I'm getting this error message after I submit the form: Warning: Cannot modify header information - headers already sent by (output started at /home/ideal/public_html/Contact_Marketing.php:160) in/home/ideal/public_html/Contact_Marketing.php on line 312 <?php require_once('recaptchalib.php'); // Get a key from http://recaptcha.net/api/getkey $publickey = "6Ldke-wSAAAAAE7yRt4cUb3G6hYU__6JIwsc6lwL"; $privatekey = "6Ldke-wSAAAAAPrYCfs4gR28Y10hEttom0ax-nKD"; # 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) { $company = $_POST['company']; $fullname = $_POST['fullname']; $address = $_POST['address']; $address2 = $_POST['address2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $phone = $_POST['phone']; $fax = $_POST['fax']; $email = $_POST['email']; $category = $_POST['category']; $message = $_POST['message']; $subject = "Marketing Inquiry"; $company = stripslashes($company); $fullname = stripslashes($fullname); $address = stripslashes($address); $city = stripslashes($city); $state = stripslashes($state); $zip = stripslashes($zip); $phone = stripslashes($phone); $fax = stripslashes($fax); $email = stripslashes($email); $category = stripslashes($category); $message = stripslashes($message); $subject = stripslashes($subject); $final_message = "You have a marketing inquiry\n\n"; $final_message .= "From: " . $fullname . "\n"; $final_message .= "Address: " . $address . "\n"; $final_message .= "Address 2: " . $address2 . "\n"; $final_message .= "City: " . $city . "\n"; $final_message .= "State: " . $state . "\n"; $final_message .= "Zip Code: " . $zip . "\n"; $final_message .= "Phone: " . $phone . "\n"; $final_message .= "Fax: " . $fax . "\n"; $final_message .= "Email: " . $email . "\n"; $final_message .= "Category: " . $category . "\n"; $final_message .= "Message: " . $message . "\n"; mail("graphics_Beth@idealwindow.com", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$final_message, "From: ".$fullname); exit(header('location:http://www.idealwindow.com/Thank_You.html')); } else { # set the error code so that we can display it $error = $resp->error; echo $error; } } echo recaptcha_get_html($publickey, $error); ?> Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464100 Share on other sites More sharing options...
TinyI Posted January 6, 2014 Share Posted January 6, 2014 I think the email your sending out is what is possibly causing the "headers already sent" error. The other thing you could try is redirecting via javascript. Not as nice/easy, but it works. $string = '<script type="text/javascript">'; $string .= 'window.location = "'http://www.urlgoeshere.../whereWeGo/'"'; $string .= '</script>'; I've never really worked with captchas, so bringing the code local and doing some tests might be a lil annoying - but if you want me to try it out, I can if you help me set up the basic page of this. Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464107 Share on other sites More sharing options...
requinix Posted January 6, 2014 Share Posted January 6, 2014 You don't even necessarily need to redirect: put that thank you page's HTML into this PHP script. Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464109 Share on other sites More sharing options...
kcelsi Posted January 6, 2014 Author Share Posted January 6, 2014 I tried the javascript method, but that also gave me errors. How would I incorporate the html into the php? Can you give an example? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464119 Share on other sites More sharing options...
requinix Posted January 6, 2014 Share Posted January 6, 2014 I tried the javascript method, but that also gave me errors. How would I incorporate the html into the php? Can you give an example? Thanks.That place where you have/had the echo? Put the HTML in there, like mail("graphics_Beth@idealwindow.com", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$final_message, "From: ".$fullname); ?> HTML here <?php ...You also need to move the reCAPTCHA bit around too: right now the thing at the end will output regardless of everything, so instead it should probably go instead an else block. } else { # set the error code so that we can display it $error = $resp->error; echo $error; } } else { echo recaptcha_get_html($publickey, $error); } Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464124 Share on other sites More sharing options...
kcelsi Posted January 6, 2014 Author Share Posted January 6, 2014 Thank you. This does work, but the issue I have is that it appears at the bottom of the page. So after the form is submitted, the top of the same page shows up and it appears that nothing has happened and you have to scroll down. I like it better when a new page comes up with the message at the top so there's no scrolling necessary to see the result. If anyone knows another way to redirect to a new page, please let me know. Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464131 Share on other sites More sharing options...
kcelsi Posted January 6, 2014 Author Share Posted January 6, 2014 I found this possible explanation here: http://stackoverflow.com/questions/8028957/headers-already-sent-by-php To understand why headers must be sent before output it's necessary to look at a typical HTTP*response. PHP scripts mainly generate HTML content, but also pass a set of HTTP/CGI headers to the webserver: HTTP/1.1 200 OKPowered-By: PHP/5.3.7Vary: Accept-EncodingContent-Type: text/html; charset=utf-8<html><head><title>PHP page output page</title></head><body><h1>Content</h1> <p>Some more output follows...</p>and <a href="/"> <img src=about:note> ... The page/output always follows the headers. PHP is required to pass the headers to the webserver first. It can only do that once. And after the double linebreak it can't ever append to them again. When PHP receives the first output (print, echo, <html>) it will "flush" the collected headers. Afterwards it can send all the output bits it wants. But sending further headers is impossible from then. How can you find out where the premature output occured?The header() warning contains all relevant information to locate the problem source: Here "line 100" refers to the script where the header() invocation failed. Warning: Cannot modify header information - headers already sent by (output started at/www/usr2345/htdocs/auth.php:52) in /www/usr2345/htdocs/index.php on line 100 The message in the inner parenthesis is more crucial. It mentions auth.php and line 52 as the source of premature output. One of the typical problem causes will be there: Print, echoIntentional output from print and echo statements will terminate the opportunity to send HTTP headers. The application flow must be restructured to avoid that. Use functions and templating schemes. Ensure header() calls occur before messages are written out. Functions that can write output include print, echo, printf, trigger_error, vprintf,ob_flush, var_dump, readfile, passthru among others and user-defined functions. My code: <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="Marketing_Contact" id="Marketing_Contact"> FORM HTML <?php ob_start(); require_once('recaptchalib.php'); // Get a key from http://recaptcha.net/api/getkey $publickey = "6Ldke-wSAAAAAE7yRt4cUb3G6hYU__6JIwsc6lwL"; $privatekey = "6Ldke-wSAAAAAPrYCfs4gR28Y10hEttom0ax-nKD"; # 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) { $company = $_POST['company']; $fullname = $_POST['fullname']; $address = $_POST['address']; $address2 = $_POST['address2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $phone = $_POST['phone']; $fax = $_POST['fax']; $email = $_POST['email']; $category = $_POST['category']; $message = $_POST['message']; $subject = "Marketing Inquiry"; $company = stripslashes($company); $fullname = stripslashes($fullname); $address = stripslashes($address); $city = stripslashes($city); $state = stripslashes($state); $zip = stripslashes($zip); $phone = stripslashes($phone); $fax = stripslashes($fax); $email = stripslashes($email); $category = stripslashes($category); $message = stripslashes($message); $subject = stripslashes($subject); $final_message = "You have a marketing inquiry\n\n"; $final_message .= "From: " . $fullname . "\n"; $final_message .= "Address: " . $address . "\n"; $final_message .= "Address 2: " . $address2 . "\n"; $final_message .= "City: " . $city . "\n"; $final_message .= "State: " . $state . "\n"; $final_message .= "Zip Code: " . $zip . "\n"; $final_message .= "Phone: " . $phone . "\n"; $final_message .= "Fax: " . $fax . "\n"; $final_message .= "Email: " . $email . "\n"; $final_message .= "Category: " . $category . "\n"; $final_message .= "Message: " . $message . "\n"; mail("graphics_Beth@idealwindow.com", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$final_message, "From: ".$fullname); exit(header('location:http://www.idealwindow.com/Thank_You.html')); } else { # set the error code so that we can display it $error = $resp->error; echo $error; } } echo recaptcha_get_html($publickey, $error); ?><input type="submit" name="Submit" id="Submit" /></form> Here is the error message: Warning: Cannot modify header information - headers already sent by (output started at /home/ideal/public_html/Contact_Marketing.php:160) in/home/ideal/public_html/Contact_Marketing.php on line 303 The red highlighted lines are 160 and 303. Can anyone explain how I can modify these lines to prevent the error? Quote Link to comment https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/#findComment-1464144 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.