-3

我在下面创建的表单通过电子邮件发送 CSV 文件中的输入数据。我遇到的问题是每次浏览器加载页面时,它都会发送 CSV 文件的空白版本,一旦提交表单,我希望它重定向到“谢谢”页面。我怎样才能做到这一点?

我在 HTML 表单之前使用的 PHP 是:

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

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


$to = "Ali@ebig.co.uk"; 
 $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: davidkirrage@gmail.com\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); 


}

?>
4

1 回答 1

3

您在顶部的行检查页面是否为表单提交,并没有针对整个功能展开。

您目前拥有:

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

但是右括号 } 应该一直在底部,就在 ?> 之前。就目前而言,您的代码正在检查是否有表单提交,如果有,它什么也不执行,因为这些括号内没有代码。完成后,它会自动开始运行所有其他代码,无论它是否是表单提交。

如果您希望页面重定向,请将以下代码放在新移动的右括号之前:

header('Location: http://www.yoursite.com/new_page.php');
于 2013-05-09T13:30:11.980 回答