簡體   English   中英

swiftmailer聯系表必填字段

[英]swiftmailer contact form required fields

我已經在swiftmailer處理的網站上建立了聯系表格。 目前,它已正確發送並帶有圖像附件和一些輸入字段。 如果留空,我該如何使某些字段“必填”並在這些字段上輸出錯誤消息? 在swiftmailer庫進入之前,這需要發生嗎?

抱歉,如果這很簡單,但是對PHP來說是新手,無法在任何地方找到快速的答案

<?php

$_SESSION["post"] = $_POST; 
$name = $_POST["Name"]; $email = $_POST["Email"]; $phone = $_POST["Phone"]; $dob = $_POST['DOBDay'] ."\t" .$_POST['DOBMonth'] ."\t" .$_POST['DOBYear'];$address = $_POST['AddressLine1'] ."\n" .$_POST['AddressLine2'] ."\n" .$_POST['PostCode'];$experience = $_POST["Experience"];$height = $_POST["Height"]; $size = $_POST["DressSize"];$bra = $_POST["Bra"];$waist = $_POST["Waist"];$hipwidest = $_POST["HipWidest"];$bicep = $_POST["Bicep"];$thigh = $_POST["Thigh"];$shoe = $_POST["Shoe"];    

require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('xxx@gmail.com')
->setPassword('xxx');



// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Be A Model application: Girls') 

// Set the From address with an associative array
->setFrom(array($email => $name))

// Set the To addresses with an associative array
->setTo(array('xxx@xxx.com', 'xxx@xxx.com' => 'contact test'))

// Give it a body
->setBody('Name: ' .$name ."\n"
.'Email: ' .$email ."\n"
.'Phone: ' .$phone ."\n"
.'Address: ' .$address ."\n"
.'DOB: ' .$dob ."\n"
.'Experience: ' .$experience ."\n"
.'Height: ' .$height ."\n"
.'Dress Size: ' .$size ."\n"
.'Bra: ' .$bra ."\n"
.'Waist: ' .$waist ."\n"
.'Hip at Widest: ' .$hipwidest ."\n"
.'Bicep: ' .$bicep ."\n"
.'Thigh: ' .$thigh ."\n"
.'Shoe Size: ' .$shoe ."\n" );


// And optionally an alternative body
//->addPart('<q>Here is the message itself</q>', 'text/html');

// Attachment  
$message->attach(
Swift_Attachment::fromPath($_FILES['fileatt']['tmp_name'])->setFilename($_FILES['fileatt']['name'])
);

// Send the message
$result = $mailer->send($message);

if ($result)
{
header('Location: http://www.modelmeasures.co.uk/thankyou.html');
}
echo $result;

?>

網站有兩種類型的數據驗證:客戶端和服務器端。

客戶端驗證-這種驗證類型通常是在完成表單時使用javascript完成的。 在提交之前,您已經在顯示無效字段旁邊的顯示“ X”或將字段顏色變為紅色的網站上看到了此內容。

客戶端驗證很有用,因為它可以使用戶在提交表單之前就知道存在問題,從而有機會對其進行更正。

服務器端驗證-您可以在此處檢查服務器收到的表單值,以確保其符合您期望的格式,不包含無效信息等。完成表單,提交表單后,您會看到此驗證在使用中它,頁面會重新加載並告訴您有錯誤。

無論是否在客戶端進行驗證,都應該執行這種類型的驗證。 禁用javascript很容易,如果您僅使用客戶端驗證,則人們可以輸入他們想要的任何內容。 這是安全隱患。

我通常要做的是設置頁面,並使用服務器端驗證。 這樣可以確保沒有安全問題,並且可以檢查用戶輸入的數據。 一旦工作正常,我還將添加客戶端javascript驗證,以使表單更加用戶友好。 通過這種方式進行javascript驗證,可以確保用戶輸入了正確的信息,但是如果出現問題或禁用了javascript,我的服務器仍然會驗證數據。

因此,要回答您的問題,您實際上至少應該進行服務器端驗證。 在Swiftmailer實際發送電子郵件之前進行驗證非常重要,這樣,如果輸入了無效數據,就不會發送電子郵件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM