Credit Counseling - Debt Consolidation Loans - Loans - MPAA - Internet Advertising

PDA

View Full Version : error message on submit


xkatx21x
Feb 18th 2008, 3:37 am
Hi guys,

I had someone write a script for me for a email form (as I know nothing about php, only html & css), but hen you press submit the next page comes up with an error


Warning: Cannot modify header information - headers already sent by (output started at D:\vhosts\solidmasonry.co.nz\httpdocs\contactForm.php:1) in D:\vhosts\solidmasonry.co.nz\httpdocs\contactForm.php on line 162


the code is

<?php

$returnURL = 'http://www.solidmasonry.co.nz/error.html';

// The url to send the user back

to if something went wrong
$successURL =

'http://www.solidmasonry.co.nz/thankyou.html';// The url to

send the user to if all goes well

$Temail = 'quote@solidmasonry.co.nz';// Change it to

anything you like -- is where the email will be sent to
$TemailBcc = 'accounts@solidmasonry.co.nz'; // If you want

to copy anyone in on all emails sent from the form, add them

here separated by commas
$Semail = '"Quote Form" <%s>';
$subject = 'Request Quote';

// If we didn't receive the correct input from the submit

button, we'll send them away straight awa

if (isset($_POST['submit']))
{

//header ('Location: ' . $returnURL);
//exit();


// Set up an array of the elements to check and place in our

message
// if you want the value to be mandatory, change the false to

true -- don't change 4=>true though

$formInputs = array('title' => array('Title', true,

array('mr', 'mrs', 'miss', 'ms', 'other')),
'firstname'

=> array('First Name', true),
'lastname'

=> array('Last Name', true),
'email'

=> array('Email Address', true, 3 => 'check_email'),
'phone'

=> array('Phone Number', false, 3 => 'ctype_digit'),
'suburb'

=> array('Suburb', true),
'work'

=> array('Work Type Request', false, array('brick',

'block', 'paving', 'concrete', 'grout', 'other')),
'work2bdone'

=> array('Work To Be Performed', false),
'day'

=> array('Best Day For Contact', false, array

('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',

'Saturday', 'Sunday'), 4 => true),
'time'

=> array('Best Time For Contact', false, array('8-

12', '12-5', '5-9', 'Any'), 4 => true),
'find'

=> array('Where You Heard About Us', false, array

('google', 'friend', 'busref', 'locpaper', 'natpaper', 'buscard',

'car_graphic', 'other'))
);


// now loop throught that new array and sort everything out

$errors = array(); // Here is our errors list -- we

will fill it as required
$errorMsg = 'Please check that you entered

<strong>%s</strong> correctly';

$messageArray = array(); // Will hold the list of values to

send out in the email
foreach ($formInputs as $pk => $pa)
{
if (!isset($_POST[$pk]) && $pa[1] === true)
{
$errors[] = sprintf($errorMsg, $pa[0]);
continue;
}



/***** Form inputs that have multiple available selections

*****/
if ($pa[4] === true && is_array($_POST[$pk])) {

$msg = '';
foreach($_POST[$pk] as $val)
{
$msg .= in_array($val, $pa[2]) ?

"\t\t" . htmlentities($val) . "<br />\n" : '';
}

if (!empty($msg))
$messageArray[] = '<strong>' .

$pa[0] . "</strong> : <br />\n\n<br />" . $msg;
else if ($pa[1] === true)
$errors[] = sprintf($errorMsg,

$pa[0]);

continue;
} else if ($pa[1] === true && $pa[4] === true) {
$errors[] = sprintf($errorMsg, $pa[0]);
continue;
}

/***** Form inputs that have a list of options to select from

******/

if (isset($pa[2]))
{

if (in_array($_POST[$pk], $pa[2]))
{
$messageArray[] = '<strong>' .

$pa[0] . '</strong> : ' . htmlentities($_POST[$pk]);
} else if ($pa[1] === true)
$errors[] = sprintf($errorMsg,

$pa[0]);

continue;
}
$_POST[$pk] = trim($_POST[$pk]);

if (empty($_POST[$pk]) && $pa[1] === true)
{
$errors[] = sprintf($errorMsg, $pa[0]);
continue;
} else if (empty($_POST[$pk]))
continue;

/**** Form inputs that require some sort of validation

(phone numbers/email) ****/

if (isset($pa[3]))
{
if ($pa[3]($_POST[$pk]))
$messageArray[] = '<strong>' .

$pa[0] . '</strong> : ' . htmlentities($_POST[$pk]);
else if ($pa[1] == true)
$errors[] = sprintf($errorMsg,

$pa[0]);

continue;
}

// must be just a normal input so add it

$messageArray[] = '<strong>' . $pa[0] . '</strong>

: ' . htmlentities($_POST[$pk]);

}


// If we didn't have any errors occur we can start to set up

the message

if (!count($errors))
{
$Semail = sprintf($Semail, $_POST['email']);

$ob = md5(time() . 'masonboundary');
$ib = md5(time() . 'masonboundary2');


$headers = "From: ".$Semail."\n";
if (!empty($TemailBcc)) $headers .= "Bcc:

".$TemailBcc."\n";
$headers .= "Reply-To: <". $_POST['email'].">\n";
$headers .= "Return-Path: <" . $_POST['email'] . ">\n";
$headers .= 'Date: ' . gmdate('D, d M Y H:i:s') . ' +0000' .

"\n";
$headers .= 'X-Mailer: Solid Masonry' . "\n";

$headers .= 'Mime-Version: 1.0' . "\n";
$headers .= 'Content-Type: multipart/alternative;

boundary="' . $ob . '"' . "\n";
$headers .= 'Content-Transfer-Encoding: 7bit' . "\n";

$cnt = $content;

$content = "<title>$Semail</title>\n\n\n";
$content .= "<body style='font-family: verdana; font-size:

9pt;'>\n\n";
$content .= "<h1 style='font-size:1.1em'>Someone Has

Contacted you!</h1>\n\n\n";

// loop through the messages

foreach ($messageArray as $msg)
$content .= $msg . "\n\n<br /><br />";

$content .= "Your Sincerely,\n\n<br /><br />";
$content .= "Your Website";


$message = strip_tags($content) . "\n" . '--' . $ob . "\n";

$message .= 'Content-Type: text/plain; charset=iso-8859

-1' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
$message .= strip_tags($content) . "\n" . '--' . $ob . "\n";

$message .= 'Content-Type: text/html;

charset=charset=iso-8859-1' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
$message .= $content . "\n" . '--' . $ob . '--';


mail($Temail, $subject, $message, $headers);
header('Location: ' . $successURL);
}

}
function check_email($email)
{ // First, we check that there's one @ symbol,
// and that the lengths are right.
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters
// in one section or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!

#$%&
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
return false;
}
}
// Check if domain is IP. If not,
// it should be valid domain name
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
↪([A-Za-z0-9]+))$",
$domain_array[$i])) {
return false;
}
}
}
return true;
}
?>

<!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>Untitled Document</title>
</head>

<body>
<?php

if (count($errors))
{
echo '<div style="border: solid 1px red;padding:

2em;">
<h1 style="font-size: 1.2em;">Some

Errors Occurred</h1>
<ul>';

foreach ($errors as $err)
echo '<li>', $err, '</li>';

echo '</ul>
<a href="', $returnURL , '"

onclick="window.history.back(); return false;">Go Back and

try again</a>
</div>';
}
?>


</body>
</html>


the error line 162 is

header('Location: ' . $successURL);
}

The email comes through ok and the error message if you forget a field is ok

if anyone has any ideas please let me know thanks.

jayshah
Feb 18th 2008, 3:40 am
Cheat a bit:

Replace


<?php


with


<?php
ob_start();


at the very top of your file, only once.

Edit: The error is caused by you outputting content then outputting the headers. Ob_start() turns on output buffering so that you can do it this way round. You are sacrificing your content not being outputted as it's generated.


Jay

James.Blant
Feb 18th 2008, 8:56 am
what is your editor ?
use EmEditor and remove 2 hidden unicode character at first line before : " <?php "

xkatx21x
Feb 18th 2008, 1:16 pm
Cheat a bit:

Replace


<?php


with


<?php
ob_start();


at the very top of your file, only once.

Edit: The error is caused by you outputting content then outputting the headers. Ob_start() turns on output buffering so that you can do it this way round. You are sacrificing your content not being outputted as it's generated.


Jay

I did this but all it did was change the error from line 162 to line 163.

James, I'm using notepad but I have just downloaded Em Editor but cannot see any hidden characters before <?php

00johnny
Feb 18th 2008, 7:08 pm
the code that you posted is chopped up, I can't really run it with it all chopped up (wrapped text). Can you repost without it wrapped?

xkatx21x
Feb 18th 2008, 7:16 pm
the code that you posted is chopped up, I can't really run it with it all chopped up (wrapped text). Can you repost without it wrapped?

<?php
ob_start();

$returnURL = 'http://www.solidmasonry.co.nz/error.html'; // The url to send the user back to if something went wrong
$successURL = 'http://www.solidmasonry.co.nz/thankyou.html'; // The url to send the user to if all goes well

$Temail = 'quote@solidmasonry.co.nz';// Change it to anything you like -- is where the email will be sent to
$TemailBcc = 'accounts@solidmasonry.co.nz'; // If you want to copy anyone in on all emails sent from the form, add them here separated by commas
$Semail = '"Quote Form" <%s>';
$subject = 'Request Quote';

// If we didn't receive the correct input from the submit button, we'll send them away straight awa

if (isset($_POST['submit']))
{

//header ('Location: ' . $returnURL);
//exit();


// Set up an array of the elements to check and place in our message
// if you want the value to be mandatory, change the false to true -- don't change 4=>true though

$formInputs = array('title' => array('Title', true, array('mr', 'mrs', 'miss', 'ms', 'other')),
'firstname' => array('First Name', true),
'lastname' => array('Last Name', true),
'email' => array('Email Address', true, 3 => 'check_email'),
'phone' => array('Phone Number', false, 3 => 'ctype_digit'),
'suburb' => array('Suburb', true),
'work' => array('Work Type Request', false, array('brick', 'block', 'paving', 'concrete', 'grout', 'other')),
'work2bdone' => array('Work To Be Performed', false),
'day' => array('Best Day For Contact', false, array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'), 4 => true),
'time' => array('Best Time For Contact', false, array('8-12', '12-5', '5-9', 'Any'), 4 => true),
'find' => array('Where You Heard About Us', false, array('google', 'friend', 'busref', 'locpaper', 'natpaper', 'buscard', 'car_graphic', 'other'))
);


// now loop throught that new array and sort everything out

$errors = array(); // Here is our errors list -- we will fill it as required
$errorMsg = 'Please check that you entered <strong>%s</strong> correctly';

$messageArray = array(); // Will hold the list of values to send out in the email
foreach ($formInputs as $pk => $pa)
{
if (!isset($_POST[$pk]) && $pa[1] === true)
{
$errors[] = sprintf($errorMsg, $pa[0]);
continue;
}



/***** Form inputs that have multiple available selections *****/
if ($pa[4] === true && is_array($_POST[$pk])) {

$msg = '';
foreach($_POST[$pk] as $val)
{
$msg .= in_array($val, $pa[2]) ? "\t\t" . htmlentities($val) . "<br />\n" : '';
}

if (!empty($msg))
$messageArray[] = '<strong>' . $pa[0] . "</strong> : <br />\n\n<br />" . $msg;
else if ($pa[1] === true)
$errors[] = sprintf($errorMsg, $pa[0]);

continue;
} else if ($pa[1] === true && $pa[4] === true) {
$errors[] = sprintf($errorMsg, $pa[0]);
continue;
}

/***** Form inputs that have a list of options to select from ******/

if (isset($pa[2]))
{

if (in_array($_POST[$pk], $pa[2]))
{
$messageArray[] = '<strong>' . $pa[0] . '</strong> : ' . htmlentities($_POST[$pk]);
} else if ($pa[1] === true)
$errors[] = sprintf($errorMsg, $pa[0]);

continue;
}
$_POST[$pk] = trim($_POST[$pk]);

if (empty($_POST[$pk]) && $pa[1] === true)
{
$errors[] = sprintf($errorMsg, $pa[0]);
continue;
} else if (empty($_POST[$pk]))
continue;

/**** Form inputs that require some sort of validation (phone numbers/email) ****/

if (isset($pa[3]))
{
if ($pa[3]($_POST[$pk]))
$messageArray[] = '<strong>' . $pa[0] . '</strong> : ' . htmlentities($_POST[$pk]);
else if ($pa[1] == true)
$errors[] = sprintf($errorMsg, $pa[0]);

continue;
}

// must be just a normal input so add it

$messageArray[] = '<strong>' . $pa[0] . '</strong> : ' . htmlentities($_POST[$pk]);

}


// If we didn't have any errors occur we can start to set up the message

if (!count($errors))
{
$Semail = sprintf($Semail, $_POST['email']);

$ob = md5(time() . 'masonboundary');
$ib = md5(time() . 'masonboundary2');


$headers = "From: ".$Semail."\n";
if (!empty($TemailBcc)) $headers .= "Bcc: ".$TemailBcc."\n";
$headers .= "Reply-To: <". $_POST['email'].">\n";
$headers .= "Return-Path: <" . $_POST['email'] . ">\n";
$headers .= 'Date: ' . gmdate('D, d M Y H:i:s') . ' +0000' . "\n";
$headers .= 'X-Mailer: Solid Masonry' . "\n";

$headers .= 'Mime-Version: 1.0' . "\n";
$headers .= 'Content-Type: multipart/alternative; boundary="' . $ob . '"' . "\n";
$headers .= 'Content-Transfer-Encoding: 7bit' . "\n";

$cnt = $content;

$content = "<title>$Semail</title>\n\n\n";
$content .= "<body style='font-family: verdana; font-size: 9pt;'>\n\n";
$content .= "<h1 style='font-size:1.1em'>Someone Has Contacted you!</h1>\n\n\n";

// loop through the messages

foreach ($messageArray as $msg)
$content .= $msg . "\n\n<br /><br />";

$content .= "Your Sincerely,\n\n<br /><br />";
$content .= "Your Website";


$message = strip_tags($content) . "\n" . '--' . $ob . "\n";

$message .= 'Content-Type: text/plain; charset=iso-8859-1' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
$message .= strip_tags($content) . "\n" . '--' . $ob . "\n";

$message .= 'Content-Type: text/html; charset=charset=iso-8859-1' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
$message .= $content . "\n" . '--' . $ob . '--';


mail($Temail, $subject, $message, $headers);
header('Location: ' . $successURL);
}

}
function check_email($email)
{ // First, we check that there's one @ symbol,
// and that the lengths are right.
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters
// in one section or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
return false;
}
}
// Check if domain is IP. If not,
// it should be valid domain name
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
↪([A-Za-z0-9]+))$",
$domain_array[$i])) {
return false;
}
}
}
return true;
}
?>

<!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>Untitled Document</title>
</head>

<body>
<?php

if (count($errors))
{
echo '<div style="border: solid 1px red;padding: 2em;">
<h1 style="font-size: 1.2em;">Some Errors Occurred</h1>
<ul>';

foreach ($errors as $err)
echo '<li>', $err, '</li>';

echo '</ul>
<a href="', $returnURL , '" onclick="window.history.back(); return false;">Go Back and try again</a>
</div>';
}
?>


</body>
</html>


Thats the best I can get it on here is that any better

If not I've added it as an attachment for you.