2013-04-09 81 views
0

我已经在swiftmailer处理的网站上建立了联系表单。目前它正确地发送图像附件和一些输入字段。如何让某些字段“必需”,并输出错误消息,如果留空?这是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('[email protected]') 
->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('[email protected]', '[email protected]' => '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; 

?> 

回答

0

有两种类型的网站,客户端和服务器端的数据验证。

客户端验证 - 这种类型的验证通常是在完成表单的情况下使用javascript完成的。在您提交之前,您在已显示'X'的网站上看到了这一点,或者在无效的表单字段旁边将字段颜色变为红色。

客户端验证非常有用,因为它可以让用户在提交表单之前就知道存在问题,并为他们提供纠正问题的机会。

服务器端验证 - 这是您在服务器收到时检查表单值以确保其格式符合您的期望,不包含无效信息等的地方。当您完成后,您会看到此验证正在使用中一个表单,提交它,并重新加载页面,并告诉你有错误。

无论您是否在客户端进行验证,您都应该进行此类验证。很容易禁用JavaScript,如果你只使用客户端验证,人们可以输入他们想要的任何东西。这是一个安全风险。

我通常会做的事情是设置我的页面,并使用服务器端验证。这确保没有安全问题,并且我正在检查用户输入的数据。一旦这是工作,我还添加客户端JavaScript验证,使表单更加用户友好。这样做的JavaScript验证确保用户输入正确的信息,但如果出现问题或JavaScript被禁用,我的服务器无论如何验证数据。

因此,要回答你的问题,你至少应该做服务器端验证。在Swiftmailer实际发送电子邮件之前进行验证非常重要,以便在输入无效数据时不会发送电子邮件。