2013-05-09 34 views
-2

我在下面創建的表單將輸入數據通過電子郵件發送到CSV文件中。 我遇到的問題是每一個瀏覽器加載頁面發送CSV文件的空白版本, 一旦提交表單,我希望它重定向到一個「謝謝你」頁面的時間。我怎樣才能做到這一點?通過瀏覽器加載電子郵件表格

我的HTML表格前,使用PHP是:

<?php 
if(isset($_POST['formSubmit'])) { 
} 

$email=$_REQUEST['email']; 
$firstName=$_REQUEST['firstName']; 
$lastName=$_REQUEST['lastName']; 


$to = "[email protected]"; 
$subject = "New application submission"; 


$message = "". 
"Email: $email" . "\n" . 
"First Name: $firstName" . "\n" . 
"Last Name: $lastName"; 


//The Attachment  
$varTitle = $_POST['formTitle'];  
$varForname = $_POST['formForname']; 
$varMiddlename = $_POST['formMiddlename']; 
$varSurname = $_POST['formSurname']; 
$varKnownas = $_POST['formKnownas']; 
$varAdressline1 = $_POST['formAdressline1']; 
$varAdressline2 = $_POST['formAdressline2']; 
$varAdressline3 = $_POST['formAdressline3']; 
$varAdressline4 = $_POST['formAdressline4']; 
$varAdressline5 = $_POST['formAdressline5']; 
$varPostcode = $_POST['formPostcode']; 
$varTelephone = $_POST['formTelephone']; 
$varMobile = $_POST['formMobile']; 
$varEmail = $_POST['formEmail']; 
$varApproval = $_POST['formApproval']; 
$varothersurname = $_POST['formothersurname']; 
$varsex = $_POST['formsex']; 
$varninumber = $_POST['formninumber']; 
$varjobtitle = $_POST['formjobtitle']; 
$vardates = $_POST['formdates']; 
$varresponsibilities = $_POST['formresponsibilities']; 
$varjobtitle2 = $_POST['formjobtitle2']; 
$vardates2 = $_POST['formdates2']; 
$varresponsibilities2 = $_POST['formresponsibilities2']; 
$varjobtitle3 = $_POST['formjobtitle3']; 
$vardates3 = $_POST['formdates3']; 
$varresponsibilities3 = $_POST['formresponsibilities3']; 
$varwebsite = $_POST['formwebsite']; 
$vartshirt = $_POST['formtshirt']; 
$vardietary = $_POST['formdietary']; 
$varpc = $_POST['formpc']; 
$varmac = $_POST['formmac']; 
$varlaptop = $_POST['formlaptop']; 
$vardongle = $_POST['formdongle']; 
$varediting = $_POST['formediting']; 
$varsocial = $_POST['formsocial']; 
$varphotography = $_POST['formphotography']; 
$varfilming = $_POST['formfilming']; 
$vartraining = $_POST['formtraining']; 
$varexhibition = $_POST['formexhibition']; 
$varspecial = $_POST['formspecial']; 
$varhobbies = $_POST['formhobbies']; 
$varphotography = $_POST['formphotography']; 
$varfilming = $_POST['formfilming']; 
$vartraining = $_POST['formtraining']; 
$varexcel = $_POST['formexcel']; 
$varbigpicture = $_POST['formbigpicture']; 
$varcriminal = $_POST['formcriminal']; 

$cr = "\n"; 
$data = "$varTitle" . ',' . "$varForname" . ',' . "$varMiddlename" . ',\\other variables. 


$attachments[] = Array( 
    'data' => $data, 
    'name' => 'application.csv', 
    'type' => 'application/vnd.ms-excel' 
); 


//Generate a boundary string 

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 


//Add the headers for a file attachment 


$headers = "MIME-Version: 1.0\n" . 
     "From: {$from}\n" . 
     "Cc: [email protected]\n". 
     "Content-Type: multipart/mixed;\n" . 
     " boundary=\"{$mime_boundary}\""; 


//Add a multipart boundary above the plain message 


$message = "This is a multi-part message in MIME format.\n\n" . 
     "--{$mime_boundary}\n" . 
     "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
     "Content-Transfer-Encoding: 7bit\n\n" . 
     $text . "\n\n"; 


//Add sttachments 

foreach($attachments as $attachment){ 
$data = chunk_split(base64_encode($attachment['data'])); 
$name = $attachment['name']; 
$type = $attachment['type']; 




$message .= "--{$mime_boundary}\n" . 
      "Content-Type: {$type};\n" . 
      " name=\"{$name}\"\n" .    
      "Content-Transfer-Encoding: base64\n\n" . 
      $data . "\n\n" ; 


$message .= "--{$mime_boundary}--\n"; 
mail($to, $subject, $message, $headers); 


} 

?> 

回答

3

你在頂部,檢查看是否頁面表單提交線,不擴大整個功能。

您目前有:

if(isset($_POST['formSubmit'])) { 
} 

但是右括號}應該是在底部一路前右>?。目前的情況是,你的代碼做一個檢查,看看是否有一個表單提交,如果是這樣它執行任何操作,因爲有那些括號內沒有任何代碼。完成之後,無論它是否是表單提交,它都會自動開始運行所有其他代碼。

如果你想要的頁面重定向,將下面的代碼新移動的閉架的前面:

header('Location: http://www.yoursite.com/new_page.php'); 
+0

用於重定向頭的PHP代碼不起作用 – user2338461 2013-05-10 08:38:29

+0

下列R- – user2338461 2013-05-10 13:21:02