473,383 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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.

http://pastebin.org/249014


process_form sends the email. The redirect is easily findable.


Thanks.

<Edit: Added the code here. External references tend to be unreliable. -- Atli>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //start session
  3. session_start();
  4.  
  5. // prints form
  6. function print_form(){
  7. ?>
  8.     <form method="post" action="<?php echo $_SERVER[’PHP_SELF’];?>" id="uploadform" enctype="multipart/form-data">
  9.     <p><label for="namefrom">Name <span class="required">*</span></label>
  10.     <input name="namefrom" id="namefrom" type="text" class="field" value="<?= $_SESSION['myForm']['namefrom']; ?>" tabindex="1"/></p>
  11.  
  12.     <p><label for="emailfrom">Email <span class="required">*</span></label>
  13.     <input name="emailfrom" id="emailfrom" type="text" class="field" value="<?= $_SESSION['myForm']['emailfrom']; ?>" tabindex="2"/></p>
  14.  
  15.  
  16.     <p><label for="address">Address</label>
  17.     <input name="address" id="address" type="text" class="field" value="<?= $_SESSION['myForm']['address']; ?>" tabindex="3"/><br/>
  18.     <input name="address2" id="address2" type="text" class="field" value="<?= $_SESSION['myForm']['address2']; ?>" tabindex="4"/></p>
  19.  
  20.  
  21.     <p><label for="phone">City</label>
  22.     <input name="city" id="city" type="text" class="field" value="<?= $_SESSION['myForm']['city']; ?>" tabindex="5"/></p>
  23.  
  24.     <p><label for="state">State</label>
  25.     <select id="state" name="state" class="field" value="<?= $_SESSION['myForm']['state']; ?>" tabindex="6">
  26.     <option value="none">Please Select State</option><br />
  27.     <option value="AL">Alabama</option><br />
  28.     <option value="AK">Alaska</option><br />
  29.     <option value="AZ">Arizona</option><br />
  30.     <option value="AR">Arkansas</option><br />
  31.     <option value="CA">California</option><br />
  32.     <option value="CO">Colorado</option><br />
  33.     <option value="CT">Connecticut</option><br />
  34.     <option value="DE">Delaware</option><br />
  35.     <option value="DC">Dist of Columbia</option><br />
  36.     <option value="FL">Florida</option><br />
  37.     <option value="GA">Georgia</option><br />
  38.     <option value="HI">Hawaii</option><br />
  39.     <option value="ID">Idaho</option><br />
  40.     <option value="IL">Illinois</option><br />
  41.     <option value="IN">Indiana</option><br />
  42.     <option value="IA">Iowa</option><br />
  43.     <option value="KS">Kansas</option><br />
  44.     <option value="KY">Kentucky</option><br />
  45.     <option value="LA">Louisiana</option><br />
  46.     <option value="ME">Maine</option><br />
  47.     <option value="MD">Maryland</option><br />
  48.     <option value="MA">Massachusetts</option><br />
  49.     <option value="MI">Michigan</option><br />
  50.     <option value="MN">Minnesota</option><br />
  51.     <option value="MS">Mississippi</option><br />
  52.     <option value="MO">Missouri</option><br />
  53.     <option value="MT">Montana</option><br />
  54.     <option value="NE">Nebraska</option><br />
  55.     <option value="NV">Nevada</option><br />
  56.     <option value="NH">New Hampshire</option><br />
  57.     <option value="NJ">New Jersey</option><br />
  58.     <option value="NM">New Mexico</option><br />
  59.     <option value="NY">New York</option><br />
  60.     <option value="NC">North Carolina</option><br />
  61.     <option value="ND">North Dakota</option><br />
  62.     <option value="OH">Ohio</option><br />
  63.     <option value="OK">Oklahoma</option><br />
  64.     <option value="OR">Oregon</option><br />
  65.     <option value="PA">Pennsylvania</option><br />
  66.     <option value="RI">Rhode Island</option><br />
  67.     <option value="SC">South Carolina</option><br />
  68.     <option value="SD">South Dakota</option><br />
  69.     <option value="TN">Tennessee</option><br />
  70.     <option value="TX">Texas</option><br />
  71.     <option value="UT">Utah</option><br />
  72.     <option value="VT">Vermont</option><br />
  73.     <option value="VA">Virginia</option><br />
  74.     <option value="WA">Washington</option><br />
  75.     <option value="WV">West Virginia</option><br />
  76.     <option value="WI">Wisconsin</option><br />
  77.     <option value="WY">Wyoming</option><br />
  78.     </select>
  79.     </p>
  80.  
  81.  
  82.     <p><label for="phone">Zip</label>
  83.     <input name="zip" id="zip" type="text" class="field" value="<?= $_SESSION['myForm']['zip']; ?>" tabindex="7"/></p>
  84.  
  85.  
  86.     <p><label for="phone">Phone</label>
  87.     <input name="phone" id="phone" type="text" class="field" value="<?= $_SESSION['myForm']['phone']; ?>" tabindex="8"/></p>
  88.  
  89.     <p><label for="comments">General Description</label>
  90.     <textarea name="comments" id="comments" rows="7" cols="10" class="field" tabindex="9"><?= $_SESSION['myForm']['comments']; ?></textarea></p>
  91.  
  92.     <span class="copysmhead">Attach your own specs here<br /></span>
  93.     <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>
  94.     <input name="attachment" id="attachment" type="file" tabindex="10">
  95.  
  96.     <p><input type="submit" name="submit" id="submit" value="Submit"  tabindex="11"/></p>
  97.     <p><input type="hidden" name="submitted"  value="true" /></p>
  98.     </form>
  99. <?php
  100. }
  101.  
  102. // enquiry form validation
  103.  
  104. function process_form() {
  105.     // Read POST request params into global vars
  106.     // FILL IN YOUR EMAIL
  107.     $to = "sspock@andrew.cmu.edu";
  108.     $subject = "[ KP ] " . "Quote Request ::" . " " . $_POST['emailfrom'];
  109.     $namefrom = trim($_POST['namefrom']);
  110.     $address = trim($_POST['address']);
  111.     $address2 = trim($_POST['address2']);
  112.     $phone = trim($_POST['phone']);
  113.     $emailfrom = trim($_POST['emailfrom']);
  114.     $comments = trim($_POST['comments']);
  115.     $state = trim($_POST['state']);
  116.     $city = trim($_POST['city']);
  117.     $zip = trim($_POST['zip']);
  118.  
  119.     // Headers
  120.     $headers = 'From: webmaster@knepperpress.com' . "\r\n" .
  121.                'Reply-To:' . " " . $_POST['emailfrom'];
  122.  
  123.  
  124.  
  125.     // Allowed file types. add file extensions WITHOUT the dot.
  126.     $allowtypes=array("doc", "pdf", "xls", "txt", "docx", "rtf", "wpd", "wps");
  127.  
  128.     // Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
  129.     $requirefile="false";
  130.  
  131.     // Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
  132.     // post_max_size, upload_max_filesize, file_uploads, max_execution_time!
  133.     // 2048kb = 2MB,       1024kb = 1MB,     512kb = 1/2MB etc..
  134.     $max_file_size="10240";
  135.  
  136.     // Thank you message
  137.     $thanksmessage='Your email has been sent, we will respond shortly.<p>&nbsp;</p>If you have more than one document to attach, submit the first document and start again.';
  138.  
  139.     $errors = array(); //Initialize error array
  140.  
  141.     //checks for a name
  142.     if (empty($_POST['namefrom']) ) {
  143.         $errors[]='You forgot to enter your name';
  144.         }
  145.  
  146.     //checks for an email
  147.     if (empty($_POST['emailfrom']) ) {
  148.         $errors[]='You forgot to enter your email';
  149.         } else {
  150.  
  151.         if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['emailfrom'])))) {
  152.             $errors[]='Please enter a valid email address';
  153.         } // if eregi
  154.     } // if empty email
  155.  
  156.      // checks for required file
  157.     // http://amiworks.co.in/talk/handling-file-uploads-in-php/
  158.     if($requirefile=="true") {
  159.         if($_FILES['attachment']['error']==4) {
  160.             $errors[]='You forgot to attach a file';
  161.         }
  162.     }
  163.  
  164.     //checks attachment file
  165.     // checks that we have a file
  166.     if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) {
  167.             // basename -- Returns filename component of path
  168.             $filename = basename($_FILES['attachment']['name']);
  169.             $ext = substr($filename, strrpos($filename, '.') + 1);
  170.             $filesize=$_FILES['attachment']['size'];
  171.             $max_bytes=$max_file_size*1024;
  172.  
  173.             //Check if the file type uploaded is a valid file type. 
  174.             if (!in_array($ext, $allowtypes)) {
  175.                 $errors[]="Invalid extension for your file: <strong>".$filename."</strong>";
  176.  
  177.         // check the size of each file
  178.         } elseif($filesize > $max_bytes) {
  179.                 $errors[]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb.";
  180.             }
  181.  
  182.     } // if !empty FILES
  183.  
  184.     if (empty($errors)) { //If everything is OK
  185.  
  186.         echo'<META HTTP-EQUIV="refresh" content="0;URL=thank_you.htm">';
  187.         function process_form();  // processes form.
  188.  
  189.         // send an email
  190.         // Obtain file upload vars
  191.         $fileatt      = $_FILES['attachment']['tmp_name'];
  192.         $fileatt_type = $_FILES['attachment']['type'];
  193.         $fileatt_name = $_FILES['attachment']['name'];
  194.  
  195.  
  196.         // create a boundary string. It must be unique
  197.           $semi_rand = md5(time());
  198.           $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  199.  
  200.           // Add the headers for a file attachment
  201.           $headers .= "\nMIME-Version: 1.0\n" .
  202.                       "Content-Type: multipart/mixed;\n" .
  203.                       " boundary=\"{$mime_boundary}\"";
  204.  
  205.           // Add a multipart boundary above the plain message
  206.           $message ="This is a multi-part message in MIME format.\n\n";
  207.           $message.="--{$mime_boundary}\n";
  208.           $message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
  209.           $message.="Content-Transfer-Encoding: 7bit\n\n";
  210.           $message.="Name: ".$namefrom."\n";
  211.           $message.="Email: ".$emailfrom."\n";
  212.           $message.="Address:\n\t" . $address . "\n\t" . $address2 . "\n\t" . $city . ", " . $state . " " . $zip . "\n";
  213.           $message.="Phone: ".$phone."\n";
  214.           $message.="General Description: ".$comments."\n\n";
  215.           $message.="\n";
  216.           $message.="[ Email Generated from KnepperPress.com ]"; 
  217.           $message.="\n\n\n";
  218.  
  219.         if (is_uploaded_file($fileatt)) {
  220.           // Read the file to be attached ('rb' = read binary)
  221.           $file = fopen($fileatt,'rb');
  222.           $data = fread($file,filesize($fileatt));
  223.           fclose($file);
  224.  
  225.           // Base64 encode the file data
  226.           $data = chunk_split(base64_encode($data));
  227.  
  228.           // Add file attachment to the message
  229.           $message .= "--{$mime_boundary}\n" .
  230.                       "Content-Type: {$fileatt_type};\n" .
  231.                       " name=\"{$fileatt_name}\"\n" .
  232.                       //"Content-Disposition: attachment;\n" .
  233.                       //" filename=\"{$fileatt_name}\"\n" .
  234.                       "Content-Transfer-Encoding: base64\n\n" .
  235.                       $data . "\n\n" .
  236.                       "--{$mime_boundary}--\n";
  237.  
  238.  
  239.  
  240.         }
  241.  
  242.  
  243.  
  244.  
  245.  
  246.         // Send the completed message
  247.  
  248.         $envs = array("HTTP_USER_AGENT", "REMOTE_ADDR", "REMOTE_HOST");
  249.         foreach ($envs as $env)
  250.         $message .= "$env: $_SERVER[$env]\n";
  251.  
  252.         if(!mail($to,$subject,$message,$headers)) {
  253.             exit("Mail could not be sent. Sorry! An error has occurred, please report this to the website administrator.\n");
  254.         } else {
  255.         //     echo '<div id="formfeedback"><h3>Thank You!</h3><p>'. $thanksmessage .'</p></div>';
  256.         //    unset($_SESSION['myForm']);
  257.         //     print_form();
  258.  
  259.  
  260.         } // end of if !mail
  261.  
  262.     } else { //report the errors
  263.         echo '<div id="formfeedback"><h3>Error!</h3><p>The following error(s) has occurred:<br />';
  264.         foreach ($errors as $msg) { //prints each error
  265.                 echo " - $msg<br />\n";
  266.             } // end of foreach
  267.         echo '</p><p>Please try again</p></div>';
  268.         print_form();
  269.     } //end of if(empty($errors))
  270.  
  271. } // end of process_form()
  272. ?>
May 18 '10 #1
1 3272
dgourd
25
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:

Expand|Select|Wrap|Line Numbers
  1. // Create the output buffer at the beginning of the script
  2. // I would put this on line 1 before the session since a session is
  3. // also a header and it will pass through the buffer anyways
  4. ob_start():
  5.  
  6. // At the end of the script, then flush the data out and end the buffer
  7. ob_end_flush();
  8.  
Once you create your buffer, then just put in the header whenever you want to do the redirect. It should look like this:
Expand|Select|Wrap|Line Numbers
  1. // Send the header using the header function
  2. // Replace example.com with the url you want to redirect to
  3. header("Location: http://www.example.com/");
  4.  
May 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: vishal | last post by:
hello friends i have one php script which generates html page containing form. what i want is submit this form using php script. pls give me some idea that how can i submit form using php...
3
by: Matt | last post by:
When people say submit the form data, does it mean the form data are submitted to web browser using either HTTP GET or POST method, and then it will redirect to another page. For example, this...
6
by: tencip | last post by:
Hi everyone, So, i've got a form that is very simple. It hold three elements: username, password, and domain. The goal here is to have the form submit the login to an administrative section...
1
by: AndrewWithy | last post by:
We have an aspx page that is submitting a form that redirects to a 3rd party website to take a payment from the customer. We have had 19 instances over a 3 day period where the user never made it...
12
by: TheOne | last post by:
In Asp.net web form under form tag there is action field that I am point to some other page, and not to same web form. When I run this page it is always pointing to itself. How do I get around...
5
by: Navillus | last post by:
Hey gang, I have a login form that is empty by default, but can be filled with values from a previous form: <input type=text maxlength="40" size="40" name="user" value="`usr`"> <input...
13
by: deko | last post by:
I have a basic feedback form with a submit button. After the "send" button is clicked, I want the user to be redirected to a different page that says "Your message has been sent." How do I do...
1
by: gbezas | last post by:
Hi All, I have added an event handler to redirect form.submit() to a newSubmit() method that I have defined (which does some additional processing before submitting the form). Additionally I...
4
by: szimek | last post by:
Hi, I've already posted an email with this problem, but this time I think I got a bit more info. The app I'm currently working on works like this: when user clicks on a clickable element, it...
11
by: MikeSA1965 | last post by:
Thank you for your patience. I have looked at numerous forums to no avail. I have a simple form html/php, for visitors to send questions. The form is on page 1, which submits to page to, echoes...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.