473,666 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 3311
dgourd
25 New Member
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
3999
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 code. it is possible to submit form using javascript or vbscript but i am not allowed to use javascript or vbscript in my project. so how can i do it using php code??
3
2081
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 is page1.aspx <form action="page2.aspx" method="post"> means it will submit the page1.aspx form data to the web server, and then redirect from page1.aspxto page2.aspx?
6
13383
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 depending on which domain someone has chosen. For instance, let's say we have three administrative sites, that all have different URLs, but we want this one form to handle logging into any of them. So, the form itself needs to have a dynamic action
1
2295
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 to the 3rd party site but were redirected back to the page that was submitted. These 19 instances came from only 3 users, with 2 users repeating multiple times in succession. All 3 users were running WinXP with IE 6.0. The web servers are...
12
3435
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 this? Here is HTML code from webform.. Thanks, Sinisa
5
17696
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 type=password maxlength="8" name="password" value="`pss`"> where usr and pss are sent from the previous form.
13
69994
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 this? I've tried using: header("Location:http://www.mysite.com/send-confirm.php");
1
10803
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 have defined the relavant function method in the code for details) The issue is that when Icall targetForm._submit() method from the newSubmit() function the page I get an 'Object doesn't support this property or method' error I am using IE...
4
2272
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 handles its onclick event and sets values of input fields in hidden form according to event data etc. The form is submitted, on the server side there's some javascript generated that is injected into a hidden frame (the same where the hidden form...
11
2995
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 the form and then redirects to index.php. The form picks up the data, and posts it on page 2 , but no email arrives in the inbox nor junk box. I whittled everything on page 2 back to // Send the mail $to =...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.