我在下面创建的表单通过电子邮件将输入数据发送到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);
}
?>
顶部的行检查页面是否为表单提交,但不扩展整个功能。
您当前有:
if(isset($_POST['formSubmit'])) {
}
但是,右括号}应该一直在底部,在?>的前面。现在,您的代码正在检查是否有表单提交,如果有,则不执行任何操作,因为括号内没有代码。完成后,它会自动开始运行所有其他代码,不管它是否是一个表单提交。
如果你想让页面重定向,把下面的代码放在新移动的右括号之前:
header('Location: http://www.yoursite.com/new_page.php');