...

View Full Version : redirect to thank you message - HELP!!



jewelsmac66
02-15-2010, 03:37 PM
I have read through several forums over the past week and have tried EVERYTHING with headers, hidden inputs, meta refresh, etc and am still not able to get my form to redirect to the thankyou.html after the form has been successfully submitted. Can anyone please help me?!? Thank you!!

Here is my current PHP form:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="jmacdesigns@gmail.com";
$subject="E-mail with attachment";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message=
stripslashes($_POST['address']).stripslashes($_POST['city']).stripslashes($_POST['zipcode']);


// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>

<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p><label>Street Address: <input type="text" name="address"></label></p>
<p><label>City: <input type="text" name="city"></label></p>
<p><label>Zip Code: <input type="text" name="zipcode"></label></p>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>
</body>
</html>

bdl
02-15-2010, 04:24 PM
Please be sure to use the PHP code tags and indent for readability when posting, it really makes a difference when troubleshooting.

Now, I don't see any redirect code here. At some point in time in your development I assume you tried to use header() (http://www.php.net/header)? I notice you have your PHP wrapped in markup (HTML). Do you understand that you cannot call header() if you have anything output to the browser, i.e. markup? Be sure to read the PHP manual entry I've linked; you cannot even have a space in your script before the use of a function such as header().

IMHO your form should be in a static HTML document that directs to a PHP script to process the form, send the mail if all form data is correct, and then redirect if the email was sent. In this way you achieve two things at once: making your application more flexible by separating the layers, and also giving you the ability to use header() to redirect.

jewelsmac66
02-15-2010, 04:36 PM
Sorry for the hard-to-read code. I just copied and pasted it from Dreamweaver, so I apologize for any difficulty.

This entire form is something I found online, so that's why it is written the way it is. How should I rewrite it in the style that you suggested?

Yes, I tried the header ('Location: http://www.jewelsdeignworks.com/thankyou.html'); but it still gave me the echo message of "Message Sent". Maybe I placed the header() line in the wrong location...?

JAY6390
02-15-2010, 04:50 PM
after the redirect you need to terminate the script, as header alone doesn't immediately stop the script and redirect. put die(); after the header location line and it should work

jewelsmac66
02-15-2010, 04:58 PM
OK, so I inserted the two lines and am getting this error message:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/e/w/jewelsmac6/html/apptools2.php:9) in /home/content/j/e/w/jewelsmac6/html/apptools2.php on line 10

Did I put the lines in the wrong place? I cant seem to figure out where these lines are supposed to go?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
header ('Location: http://www.jewelsdeignworks.com/thankyou.html');
die ();

if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="jmacdesigns@gmail.com";
$subject="E-mail with attachment";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message=
stripslashes($_POST['address']).stripslashes($_POST['city']).stripslashes($_POST['zipcode']);


// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>

<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p><label>Street Address: <input type="text" name="address"></label></p>
<p><label>City: <input type="text" name="city"></label></p>
<p><label>Zip Code: <input type="text" name="zipcode"></label></p>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>
</body>
</html>

JAY6390
02-15-2010, 05:00 PM
can you please put your code inside

Here's an article on how to correct headers sent errors
http://www.jaygilford.com/php/headers-already-sent-error-php-solution/

jewelsmac66
02-15-2010, 05:11 PM
I'm so sorry for the confusion, I thought my code was inside the PHP area...? I went to your link and copied and pasted your exact code and inserted it after the <?php at the top (lines 10 and 11) and it still is giving me the error message. I am very confused as to where to put these header() and die() lines.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
header('Location: newpage.php');
die ();
if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="jmacdesigns@gmail.com";
$subject="E-mail with attachment";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message=
stripslashes($_POST['address']).stripslashes($_POST['city']).stripslashes($_POST['zipcode']);


// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>

<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p><label>Street Address: <input type="text" name="address"></label></p>
<p><label>City: <input type="text" name="city"></label></p>
<p><label>Zip Code: <input type="text" name="zipcode"></label></p>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>
</body>
</html>

tomws
02-15-2010, 05:47 PM
These patient guys have asked twice for you to wrap your code in appropriate tags (http://www.codingforums.com/showthread.php?t=68462). If you'll observe that simple request, you're more likely to receive helpful assistance.

jewelsmac66
02-15-2010, 05:52 PM
Everyone, I am very sorry for not wrapping the code properly. I did not understand what that meant. As you can tell, I am totally new to PHP and this forum. I greatly appreciate your patience and help. I hope I did this right...



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
header('Location: newpage.php');
die ();
if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="jmacdesigns@gmail.com";
$subject="E-mail with attachment";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message=
stripslashes($_POST['address']).stripslashes($_POST['city']).stripslashes($_POST['zipcode']);


// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>

<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p><label>Street Address: <input type="text" name="address"></label></p>
<p><label>City: <input type="text" name="city"></label></p>
<p><label>Zip Code: <input type="text" name="zipcode"></label></p>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>
</body>
</html>

JAY6390
02-15-2010, 05:56 PM
OK Well the errors aren't PHP related, it's because you output HTML before the opening <?php tag at the start. You need to put that output AFTER the header code otherwise you will continue to get the headers already sent messages

tomws
02-15-2010, 06:01 PM
Much better!

Now, that "headers already sent" message means you've sent output to the browser before calling the header() function. You've done that here:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
header('Location: newpage.php');
Everything above your header function (outside of the php tag) is output. That means your page is structured incorrectly. Another problem, you're redirecting but not conditionally. Think through that logic. It doesn't flow like you probably initially expect.

jewelsmac66
02-15-2010, 06:01 PM
Thank you so much for your reply. I greatly appreciate your time here. I think I understand what you said - is this how the code should read then, in order for the form to redirect to the thankyou.html page after the user clicks on the submit button?



<?php
header('Location: http://www.jewelsdesignworks.com/thankyou.html');
die ();

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="jmacdesigns@gmail.com";
$subject="E-mail with attachment";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message=
stripslashes($_POST['address']).stripslashes($_POST['city']).stripslashes($_POST['zipcode']);


// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>

<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p><label>Street Address: <input type="text" name="address"></label></p>
<p><label>City: <input type="text" name="city"></label></p>
<p><label>Zip Code: <input type="text" name="zipcode"></label></p>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>
</body>
</html>

bdl
02-15-2010, 06:16 PM
<?php
header('Location: http://www.jewelsdesignworks.com/thankyou.html');
die ();


What you're doing here is technically correct, in that header() is called prior to anything being sent to the script. Unfortunately this is not what you want. When this script is called, all it will ever to is redirect, because the first line is a call to header() and the second line kills the script. What you need to do is process the script and then call header().

See my example below in response to:

This entire form is something I found online, so that's why it is written the way it is. How should I rewrite it in the style that you suggested?

main document with the form:


<!DOCTYPE html>
<html>
<head>
...

<form action="formhandler.php">

</form>
...
</html>


form handler script:


<?php
// formhandler.php
if ( sizeof($_POST) > 1 ) {
// process & validate form

// if all data is present and "clean", then proceed
if ( mail() ) {
// mail success!
header("Location: thankyou.html");
} else {
// mail failure!
header("Location: tryagain.html");
}

}
?>


Of course this is a glib example of what you need to do, but it's the concept of separation I was referring to. You see how the PHP script does nothing but process data - it doesn't handle the HTML at all. Then it can redirect using a call to header() without any problems.

jewelsmac66
02-15-2010, 06:27 PM
I think we are almost there...! Now I am getting this error, but I don't see how I can delete the '<' since its being used for the <!DOCTYPE html>, right?


Parse error: syntax error, unexpected '<' in /home/content/j/e/w/jewelsmac6/html/apptools2.php on line 5




<?php
header('Location: http://www.jewelsdesignworks.com/thankyou.html');
die ();

<!DOCTYPE html>
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="jmacdesigns@gmail.com";
$subject="E-mail with attachment";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message=
stripslashes($_POST['address']).stripslashes($_POST['city']).stripslashes($_POST['zipcode']);


// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
// formhandler.php
if ( sizeof($_POST) > 1 ) {
// process & validate form

// if all data is present and "clean", then proceed
if ( mail() ) {
// mail success!
header("Location: thankyou.html");
} else {
// mail failure!
header("Location: tryagain.html");
}

}
?>

<p>Send an e-mail with an attachment:</p>
<form action="formhandler.php" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p><label>Street Address: <input type="text" name="address"></label></p>
<p><label>City: <input type="text" name="city"></label></p>
<p><label>Zip Code: <input type="text" name="zipcode"></label></p>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
</body>
</html>

bdl
02-15-2010, 06:30 PM
You're not closing the opening PHP tag. This is not your only problem, however. Please read my last post.


<?php
header('Location: http://www.jewelsdesignworks.com/thankyou.html');
die ();
?>
<!DOCTYPE html>

jewelsmac66
02-15-2010, 06:41 PM
Oh ok, the last post didnt have the ?> part. When I did insert the ?> I also had to add another <?php above the if() line.

So, after I updated the code now its saying this:

Parse error: syntax error, unexpected $end in /home/content/j/e/w/jewelsmac6/html/apptools2.php on line 122

Line 122 is the very last line in the code, </html>. This doesnt make sense to me either.



<?php
header('Location: http://www.jewelsdesignworks.com/thankyou.html');
die ();
?>
<!DOCTYPE html>
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="jmacdesigns@gmail.com";
$subject="E-mail with attachment";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message=
stripslashes($_POST['address']).stripslashes($_POST['city']).stripslashes($_POST['zipcode']);


// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
// formhandler.php
if ( sizeof($_POST) > 1 ) {
// process & validate form

// if all data is present and "clean", then proceed
if ( mail() ) {
// mail success!
header("Location: thankyou.html");
} else {
// mail failure!
header("Location: tryagain.html");
}

}
?>

<p>Send an e-mail with an attachment:</p>
<form action="formhandler.php" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p><label>Street Address: <input type="text" name="address"></label></p>
<p><label>City: <input type="text" name="city"></label></p>
<p><label>Zip Code: <input type="text" name="zipcode"></label></p>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
</body>
</html>

tomws
02-16-2010, 03:22 PM
"unexpected $end" often means you have some mismatched curly braces.

JAY6390
02-16-2010, 03:25 PM
Yep

if ($_SERVER['REQUEST_METHOD'] == "POST") {
is missing it's closing brace



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum