Jump to content

Recommended Posts

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');
 
$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);
?>
Link to comment
https://forums.phpfreaks.com/topic/285142-open-new-page-using-a-captcha/
Share on other sites

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');
 
$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);
        } else {
                # set the error code so that we can display it
                $error = $resp->error; echo $error;
        }
}
echo recaptcha_get_html($publickey, $error);
?>

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');
 
$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);
        } else {
                # set the error code so that we can display it
                $error = $resp->error; echo $error;
        }
}
echo recaptcha_get_html($publickey, $error);
?>

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.

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);
}

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.

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 OK
Powered-By: PHP/5.3.7
Vary: Accept-Encoding
Content-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, echo

Intentional 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');
$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);
        } 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? 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.