通过电子邮件发送浏览器加载

时间:2013-05-09 13:14:59

标签: php email redirect load

我在下面创建的表单通过电子邮件将输入数据发送到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); 


}

?>

1 个答案:

答案 0 :(得分:3)

顶部的行(检查页面是否为表单提交)不会针对整个功能进行扩展。

您目前有:

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

然而,结束括号}应该一直在底部,就在?&gt;之前。现在,您的代码正在检查表单是否有表单提交,如果是,则执行任何操作,因为这些括号中没有代码。完成后,它会自动开始运行所有其他代码,无论它是否是表单提交。

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

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