Jump to content

PHP E-Mail/Feedback Form Help


xt gohan

Recommended Posts

I was wondering if you guys could help me out with this problem I'm having:

 

Warning: Cannot modify header information - headers already sent by (output started at /home1/theninja/public_html/index.php:7) in /home1/theninja/public_html/contact/mailer.php on line 22

 

I recently started using an e-mail feedback form but apparently my scripts are conflicting in some manner. This code is from the file that conflicts, I have another file that holds the form, if you guys need to look at that, I can post that here as well.

<?php

// load the variables form address bar
$subject = $_REQUEST["subject"];
$message = $_REQUEST["message"];
$from = $_REQUEST["from"];
$verif_box = $_REQUEST["verif_box"];

// remove the backslashes that normally appears when entering " or '
$message = stripslashes($message);
$subject = stripslashes($subject);
$from = stripslashes($from);

// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
   // if verification code was correct send the message and show this page
   mail("[email protected]", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "From: $from");
   // delete the cookie so it cannot sent again by refreshing this page
   setcookie('tntcon','');
} else {
   // if verification code was incorrect then return to contact page and show error
   header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
   exit;
}
?>

 

Because I get the errors in the following quotation. And line 22 is referring to: header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true"); right after the } else {.

 

Is it possible to rewrite this in a way it doesn't screw with my original headers. The truth of the matter is that I'm using php include() to use this file as a partial on to my main index page. So I believe it conflicts because the headers are being stated twice but I'm not sure what to do from here. So if you guys can help me out, I'd be forever in your debt? :D

Link to comment
https://forums.phpfreaks.com/topic/118983-php-e-mailfeedback-form-help/
Share on other sites

Use output buffering by placing an ob_start() at the very first beginning of the page (the main page, not the included one) and ob_end_clean() at the end. In that way you buffer the output, including the header() call and send it when ure done with it (at the end of the page obviosly). Or bypass this stuff by using javascript's "window.location" or even better meta refresh instead of header().

Thanks for helping me out. How would I do this with javascript or meta? I tried using what you gave me:

 

<?php ob_start (); 
ob_end_clean (); 
?>

 

But unfortunately, it wasn't working for me and gave me a blank page. I'm not too skilled of a programmer with PHP and this is my first time meddling with anything this advanced.

For the output buffering:

 

<?php
ob_start();
?>
<html><head></head>
<body><?php include('email-code.php'); ?></body>
</html>
<?php ob_end_clean(); ?>

 

While the meta refresh method should be:

<?php
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
     //do some stuff
} else{
     echo "<meta http-equiv='refresh' content='0;URL=" . $_SERVER['HTTP_REFERER'] . "?subject=$subject&from=$from&message=$message&wrong_code=true'>";
     exit;
}
?>

 

Try those out.

Thanks alot, I really appreciate your help. Well, the first one didn't work it just ended up not showing anything but a blank page. I think it might have to do with the fact that I have a login-script. Anyway, the second one worked for me sort of. But I've narrowed the problem down quite a bit:

 

When I have this it doesn't work out correctly and the e-mail ends up stuck in the address bar somehow.

 

<form action="index.php?p=mailer">

 

However it works fine when I have it set to this:

 

<form action="contact/mailer.php">

 

I believe it's because there's a conflict between the dynamic url I use and the one that's originally implemented to run the script. Although, I'm not too sure about that, it's just a guess. However, if you need me to, I can post the rest of the form script I am using.

If im getting this right, u have a form which submits the message and redirects to "index.php?p=mailer". In the original script, the form submits the message to "contact/mailer.php" which must be the script u posted in the first post. U have rather 2 options, have the action as "contact/mailer.php" and redirect the input data to that script, or set the action to the actual page (or just leave it empty). The first option is straightforward, while for the second one u should modify your script to something like this:

 

contact.php (as a page example)

<?php
if(isset($_POST['message'])){ //check if the form is submitted
     $subject = $_REQUEST["subject"];
     $message = $_REQUEST["message"];
     $from = $_REQUEST["from"];
     $verif_box = $_REQUEST["verif_box"];

     $message = stripslashes($message);
     $subject = stripslashes($subject);
     $from = stripslashes($from);

    if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
         mail("[email protected]", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "From: $from");
         setcookie('tntcon','');
         echo 'The message was sent successfuly.';
    } else {
         echo 'The security code is incorrect.';
    }
}
?>

 

Thats basically the exact script which u posted in the first post, but put in a condition and added some echos. U can put this in the same page where u have the post, somewhere where u will show the error/success messages. Consider also adding some validation for the other fields (message, subject and such) so u dont end up with empty messges.

 

U should figure it out with easy, but if u still have problems, just ask.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.