homepage Welcome to WebmasterWorld Guest from 54.205.7.136
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Redirect error
trying to email self and redirect
pixies




msg:4609169
 4:24 am on Sep 12, 2013 (gmt 0)

I'm a bit rusty, it has been years since coded and just recently picked it back up. I am trying to email myself and direct upon form submit button.

I am getting the following error:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/02/8381802/html/yourownboss/send_form_email.php:2) in /home/content/02/8381802/html/yourownboss/send_form_email.php on line 61

_______________________

PHP CODE:

<?php
if(isset($_POST['email'])) {

// Email address and subject
$email_to = "abc@yahoo.com";
$email_subject = "subject of email";


function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";


// Mail to yourself and redirect
$sent = mail($email_to, $email_subject, $email_message);

if ($sent) {
header("Location: http://www.example.com/Secondfolder/");
exit();
} else {
print "We encountered an error sending your mail";
}

}

?>


<?php

?>

_______________________

HTML CODE

<form name="contactform" method="post" action="send_form_email.php">

<table width="450px">
<tr>
<td valign="top">
<label for="first_name">First Name:</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="last_name">Last Name:</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address:</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="referral">How did you find us?<br />
(ex: Online or Al Dow)
</label>
</td>
<td valign="top">
<input type="text" name="referral" maxlength="30" size="30">
</td>
</tr>
<tr>

</tr>
<tr>
<td colspan="2" style="text-align:center">

<input type="submit" value="Get More Information"> <a href="http://www.example.com/Secondfolder/"></a>

</td>
</tr>
</table>
</form>

[edited by: phranque at 7:51 am (utc) on Sep 13, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]

 

JD_Toims




msg:4609176
 5:24 am on Sep 12, 2013 (gmt 0)

Okay, well after copy/pasting your code into TextWrangler and looking at line 61 where you're getting the error and not seeing anything that should cause the error you're receiving my best guess is it's white-space on a line that's being output prior to the redirect.

The forum removes extra spaces, so I might not see it through a copy and paste even with "invisibles" turned on -- I could be missing something and if someone else sees it I hope they feel free to point it out, but if not, the first thing I would probably check for is an empty line that starts with [or contains] [space].

If that didn't work, I'd comment out everything between the $email_to + $email_subject lines and the $sent + if($sent) conditional -- Then if that worked I'd add functions/if's back in one at a time to find which is causing the issue. If the initial commenting didn't work and I still received an error I'd only have 8 or 10 lines of live code to look at and figure out the issue with.

BTW: Welcome [or welcome back to] WebmasterWorld!

omoutop




msg:4609217
 9:17 am on Sep 12, 2013 (gmt 0)

don't forget to check for bom characters [en.wikipedia.org]

pixies




msg:4609280
 2:33 pm on Sep 12, 2013 (gmt 0)

Thanks for the post and suggestions. I do as you suggested and post my results.

Thanks again for your help!

pixies




msg:4609333
 4:31 pm on Sep 12, 2013 (gmt 0)

Thanks JD_Tomis & omoutop!

There were some white spaces and I had to change the location: http:// line from "xxx.com" to 'xxx.com'.

It is working perfectly now.

JD_Toims




msg:4609338
 5:23 pm on Sep 12, 2013 (gmt 0)

Awesome -- Glad you got it working!

pixies




msg:4609437
 3:19 am on Sep 13, 2013 (gmt 0)

Another quick question.

I have the same code in another directory but it isn't sending the email. Any thoughts why this might be?

Directory set up as follows:

Root
- index
- send_form_email
* Second Folder
- index
- send_form_email2
* Third Folder
- thankyou

pixies




msg:4609438
 3:20 am on Sep 13, 2013 (gmt 0)

p.s. Also, I changed the reference in the html to <form name="contactform" method="post" action="send_form_email2.php">

phranque




msg:4609637
 8:12 pm on Sep 13, 2013 (gmt 0)

welcome to WebmasterWorld, pixies!


I have the same code in another directory but it isn't sending the email.

what response did you get?
did the POST request actually go to the correct directory/script?
did you look for clues in your server error log?

pixies




msg:4609806
 7:54 pm on Sep 14, 2013 (gmt 0)

No error, the page directs to the thank you page. However, the email is never sent. That is the only issue.

JD_Toims




msg:4609816
 9:25 pm on Sep 14, 2013 (gmt 0)

Each line should be separated with a CRLF (\r\n)

[php.net...]

$email_message = "Form details below.\n\n";

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";

The above should technically be:

$email_message = "Form details below.\r\n\r\n";

$email_message .= "First Name: ".clean_string($first_name)."\r\n";
$email_message .= "Last Name: ".clean_string($last_name)."\r\n";
$email_message .= "Email: ".clean_string($email_from)."\r\n";

However, the email is never sent.

if ($sent) {
header("Location: http://www.example.com/Secondfolder/");
exit();
} else {
print "We encountered an error sending your mail";
}

There's no redirect in the else, so to get to the thank you page the $sent must be TRUE, meaning the mail is being sent -- My guess is by not setting a From and having a short message the mail is either going to spam or not even being delivered by the server. I've noticed in testing before if I make my subject Testing and the message even as short as Testing Testing Testing the mail is not delivered, but I if set a From and use a longer subject and message body [EG Hey, this is a test -- Wow! If I get this one it means it's working!] then it will get through the filters.

pixies




msg:4609997
 3:08 am on Sep 16, 2013 (gmt 0)

Thanks! I will give that a try and report back. :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved