How to redirect a form after submit(form sends email)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • metalforever
    New Member
    • May 2010
    • 1

    How to redirect a form after submit(form sends email)

    How do you redirect a form after submit(form sends email) ? This is pretty urgent for me. I have included the code in a pastebin.

    Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.



    process_form sends the email. The redirect is easily findable.


    Thanks.

    <Edit: Added the code here. External references tend to be unreliable. -- Atli>
    [code=php]<?php
    //start session
    session_start() ;

    // prints form
    function print_form(){
    ?>
    <form method="post" action="<?php echo $_SERVER[’PHP_SELF’];?>" id="uploadform " enctype="multip art/form-data">
    <p><label for="namefrom"> Name <span class="required ">*</span></label>
    <input name="namefrom" id="namefrom" type="text" class="field" value="<?= $_SESSION['myForm']['namefrom']; ?>" tabindex="1"/></p>

    <p><label for="emailfrom" >Email <span class="required ">*</span></label>
    <input name="emailfrom " id="emailfrom" type="text" class="field" value="<?= $_SESSION['myForm']['emailfrom']; ?>" tabindex="2"/></p>


    <p><label for="address">A ddress</label>
    <input name="address" id="address" type="text" class="field" value="<?= $_SESSION['myForm']['address']; ?>" tabindex="3"/><br/>
    <input name="address2" id="address2" type="text" class="field" value="<?= $_SESSION['myForm']['address2']; ?>" tabindex="4"/></p>


    <p><label for="phone">Cit y</label>
    <input name="city" id="city" type="text" class="field" value="<?= $_SESSION['myForm']['city']; ?>" tabindex="5"/></p>

    <p><label for="state">Sta te</label>
    <select id="state" name="state" class="field" value="<?= $_SESSION['myForm']['state']; ?>" tabindex="6">
    <option value="none">Pl ease Select State</option><br />
    <option value="AL">Alab ama</option><br />
    <option value="AK">Alas ka</option><br />
    <option value="AZ">Ariz ona</option><br />
    <option value="AR">Arka nsas</option><br />
    <option value="CA">Cali fornia</option><br />
    <option value="CO">Colo rado</option><br />
    <option value="CT">Conn ecticut</option><br />
    <option value="DE">Dela ware</option><br />
    <option value="DC">Dist of Columbia</option><br />
    <option value="FL">Flor ida</option><br />
    <option value="GA">Geor gia</option><br />
    <option value="HI">Hawa ii</option><br />
    <option value="ID">Idah o</option><br />
    <option value="IL">Illi nois</option><br />
    <option value="IN">Indi ana</option><br />
    <option value="IA">Iowa </option><br />
    <option value="KS">Kans as</option><br />
    <option value="KY">Kent ucky</option><br />
    <option value="LA">Loui siana</option><br />
    <option value="ME">Main e</option><br />
    <option value="MD">Mary land</option><br />
    <option value="MA">Mass achusetts</option><br />
    <option value="MI">Mich igan</option><br />
    <option value="MN">Minn esota</option><br />
    <option value="MS">Miss issippi</option><br />
    <option value="MO">Miss ouri</option><br />
    <option value="MT">Mont ana</option><br />
    <option value="NE">Nebr aska</option><br />
    <option value="NV">Neva da</option><br />
    <option value="NH">New Hampshire</option><br />
    <option value="NJ">New Jersey</option><br />
    <option value="NM">New Mexico</option><br />
    <option value="NY">New York</option><br />
    <option value="NC">Nort h Carolina</option><br />
    <option value="ND">Nort h Dakota</option><br />
    <option value="OH">Ohio </option><br />
    <option value="OK">Okla homa</option><br />
    <option value="OR">Oreg on</option><br />
    <option value="PA">Penn sylvania</option><br />
    <option value="RI">Rhod e Island</option><br />
    <option value="SC">Sout h Carolina</option><br />
    <option value="SD">Sout h Dakota</option><br />
    <option value="TN">Tenn essee</option><br />
    <option value="TX">Texa s</option><br />
    <option value="UT">Utah </option><br />
    <option value="VT">Verm ont</option><br />
    <option value="VA">Virg inia</option><br />
    <option value="WA">Wash ington</option><br />
    <option value="WV">West Virginia</option><br />
    <option value="WI">Wisc onsin</option><br />
    <option value="WY">Wyom ing</option><br />
    </select>
    </p>


    <p><label for="phone">Zip </label>
    <input name="zip" id="zip" type="text" class="field" value="<?= $_SESSION['myForm']['zip']; ?>" tabindex="7"/></p>


    <p><label for="phone">Pho ne</label>
    <input name="phone" id="phone" type="text" class="field" value="<?= $_SESSION['myForm']['phone']; ?>" tabindex="8"/></p>

    <p><label for="comments"> General Description</label>
    <textarea name="comments" id="comments" rows="7" cols="10" class="field" tabindex="9"><? = $_SESSION['myForm']['comments']; ?></textarea></p>

    <span class="copysmhe ad">Attach your own specs here<br /></span>
    <p class="copy" style="font-weight: normal;">(1 file only, max file size 10mb. Allowed file formats are .doc .pdf .xls .docx .txt .rtf .wpd .wps)<br/>Thank You!</p>
    <input name="attachmen t" id="attachment " type="file" tabindex="10">

    <p><input type="submit" name="submit" id="submit" value="Submit" tabindex="11"/></p>
    <p><input type="hidden" name="submitted " value="true" /></p>
    </form>
    <?php
    }

    // enquiry form validation

    function process_form() {
    // Read POST request params into global vars
    // FILL IN YOUR EMAIL
    $to = "sspock@andrew. cmu.edu";
    $subject = "[ KP ] " . "Quote Request ::" . " " . $_POST['emailfrom'];
    $namefrom = trim($_POST['namefrom']);
    $address = trim($_POST['address']);
    $address2 = trim($_POST['address2']);
    $phone = trim($_POST['phone']);
    $emailfrom = trim($_POST['emailfrom']);
    $comments = trim($_POST['comments']);
    $state = trim($_POST['state']);
    $city = trim($_POST['city']);
    $zip = trim($_POST['zip']);

    // Headers
    $headers = 'From: webmaster@knepp erpress.com' . "\r\n" .
    'Reply-To:' . " " . $_POST['emailfrom'];



    // Allowed file types. add file extensions WITHOUT the dot.
    $allowtypes=arr ay("doc", "pdf", "xls", "txt", "docx", "rtf", "wpd", "wps");

    // Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
    $requirefile="f alse";

    // Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
    // post_max_size, upload_max_file size, file_uploads, max_execution_t ime!
    // 2048kb = 2MB, 1024kb = 1MB, 512kb = 1/2MB etc..
    $max_file_size= "10240";

    // Thank you message
    $thanksmessage= 'Your email has been sent, we will respond shortly.<p>&nbs p;</p>If you have more than one document to attach, submit the first document and start again.';

    $errors = array(); //Initialize error array

    //checks for a name
    if (empty($_POST['namefrom']) ) {
    $errors[]='You forgot to enter your name';
    }

    //checks for an email
    if (empty($_POST['emailfrom']) ) {
    $errors[]='You forgot to enter your email';
    } else {

    if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(tr im($_POST['emailfrom'])))) {
    $errors[]='Please enter a valid email address';
    } // if eregi
    } // if empty email

    // checks for required file
    // http://amiworks.co.in/talk/handling-file-uploads-in-php/
    if($requirefile =="true") {
    if($_FILES['attachment']['error']==4) {
    $errors[]='You forgot to attach a file';
    }
    }

    //checks attachment file
    // checks that we have a file
    if((!empty($_FI LES["attachment "])) && ($_FILES['attachment']['error'] == 0)) {
    // basename -- Returns filename component of path
    $filename = basename($_FILE S['attachment']['name']);
    $ext = substr($filenam e, strrpos($filena me, '.') + 1);
    $filesize=$_FIL ES['attachment']['size'];
    $max_bytes=$max _file_size*1024 ;

    //Check if the file type uploaded is a valid file type.
    if (!in_array($ext , $allowtypes)) {
    $errors[]="Invalid extension for your file: <strong>".$file name."</strong>";

    // check the size of each file
    } elseif($filesiz e > $max_bytes) {
    $errors[]= "Your file: <strong>".$file name."</strong> is to big. Max file size is ".$max_file_siz e."kb.";
    }

    } // if !empty FILES

    if (empty($errors) ) { //If everything is OK

    echo'<META HTTP-EQUIV="refresh" content="0;URL= thank_you.htm"> ';
    function process_form(); // processes form.

    // send an email
    // Obtain file upload vars
    $fileatt = $_FILES['attachment']['tmp_name'];
    $fileatt_type = $_FILES['attachment']['type'];
    $fileatt_name = $_FILES['attachment']['name'];


    // create a boundary string. It must be unique
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Bo undary_x{$semi_ rand}x";

    // Add the headers for a file attachment
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mi me_boundary}\"" ;

    // Add a multipart boundary above the plain message
    $message ="This is a multi-part message in MIME format.\n\n";
    $message.="--{$mime_boundary }\n";
    $message.="Cont ent-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message.="Cont ent-Transfer-Encoding: 7bit\n\n";
    $message.="Name : ".$namefrom."\n ";
    $message.="Emai l: ".$emailfrom."\ n";
    $message.="Addr ess:\n\t" . $address . "\n\t" . $address2 . "\n\t" . $city . ", " . $state . " " . $zip . "\n";
    $message.="Phon e: ".$phone."\ n";
    $message.="Gene ral Description: ".$comments."\n \n";
    $message.="\n";
    $message.="[ Email Generated from KnepperPress.co m ]";
    $message.="\n\n \n";

    if (is_uploaded_fi le($fileatt)) {
    // Read the file to be attached ('rb' = read binary)
    $file = fopen($fileatt, 'rb');
    $data = fread($file,fil esize($fileatt) );
    fclose($file);

    // Base64 encode the file data
    $data = chunk_split(bas e64_encode($dat a));

    // Add file attachment to the message
    $message .= "--{$mime_boundary }\n" .
    "Content-Type: {$fileatt_type} ;\n" .
    " name=\"{$fileat t_name}\"\n" .
    //"Content-Disposition: attachment;\n" .
    //" filename=\"{$fi leatt_name}\"\n " .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary }--\n";



    }





    // Send the completed message

    $envs = array("HTTP_USE R_AGENT", "REMOTE_ADD R", "REMOTE_HOS T");
    foreach ($envs as $env)
    $message .= "$env: $_SERVER[$env]\n";

    if(!mail($to,$s ubject,$message ,$headers)) {
    exit("Mail could not be sent. Sorry! An error has occurred, please report this to the website administrator.\ n");
    } else {
    // echo '<div id="formfeedbac k"><h3>Thank You!</h3><p>'. $thanksmessage .'</p></div>';
    // unset($_SESSION['myForm']);
    // print_form();


    } // end of if !mail

    } else { //report the errors
    echo '<div id="formfeedbac k"><h3>Error !</h3><p>The following error(s) has occurred:<br />';
    foreach ($errors as $msg) { //prints each error
    echo " - $msg<br />\n";
    } // end of foreach
    echo '</p><p>Please try again</p></div>';
    print_form();
    } //end of if(empty($error s))

    } // end of process_form()
    ?>[/code]
    Last edited by Atli; May 19 '10, 01:20 AM. Reason: Added the code to the post.
  • dgourd
    New Member
    • Apr 2010
    • 25

    #2
    Normally to do this you would have to send a header, but headers must always be sent before anything else to the browser or you will get an error. To fix this, I create an output buffer at the beginning of my script and flush the data at the end. This buffer only sends the data to the browser once you flush the data. This allows any headers to be sent before anything and avoid any errors. So you want to do this first:

    Code:
    // Create the output buffer at the beginning of the script
    // I would put this on line 1 before the session since a session is
    // also a header and it will pass through the buffer anyways
    ob_start():
    
    // At the end of the script, then flush the data out and end the buffer
    ob_end_flush();
    Once you create your buffer, then just put in the header whenever you want to do the redirect. It should look like this:
    Code:
    // Send the header using the header function
    // Replace example.com with the url you want to redirect to
    header("Location: http://www.example.com/");

    Comment

    Working...