Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    redirect to thank you message - HELP!!

    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>

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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()? 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.

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Redirect header

    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...?

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    header() and die() lines

    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>

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    can you please put your code inside [PHP] [/PHP]
    Here's an article on how to correct headers sent errors
    http://www.jaygilford.com/php/header...-php-solution/
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    header

    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>

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    These patient guys have asked twice for you to wrap your code in appropriate tags. If you'll observe that simple request, you're more likely to receive helpful assistance.
    Are you a Help Vampire?

  • #9
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php tags

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

    PHP Code:
    <!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>

  • #10
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #11
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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:
    PHP Code:
    <!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.
    Are you a Help Vampire?

  • #12
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    headers

    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 Code:
    <?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>

  • #13
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    PHP Code:
    <?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:
    Quote Originally Posted by jewelsmac66 View Post
    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:
    Code:
    <!DOCTYPE html>
    <html>
     <head>
     ...
    
    <form action="formhandler.php">
    
    </form>
     ...
    </html>
    form handler script:
    PHP Code:
    <?php
    // formhandler.php
    if ( sizeof($_POST) > ) {
      
    // 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.

  • #14
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    almost there!

    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 Code:
    <?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) > ) {
     
    // 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>

  • #15
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You're not closing the opening PHP tag. This is not your only problem, however. Please read my last post.
    PHP Code:
    <?php
        header
    ('Location: http://www.jewelsdesignworks.com/thankyou.html');
        die ();
    ?>
    <!DOCTYPE html>


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •