PHP POST表单验证错误

时间:2013-06-26 13:08:58

标签: php html forms validation

我有一个在联系表单页面上执行的PHP表单。我已经运行良好 - 但它目前只检查以确保输入名称和电子邮件。我试图让它来检查一条消息,但我的尝试只是在php之后不加载页面。这就是我所拥有的:

<?php 
 $to = "me@gmail.com" ; 
 $from = $_REQUEST['Email'] ; 
 $name = $_REQUEST['Name'] ; 
 $headers = "From: $from"; 
 $subject = "Web Contact Data";
 $startmonth = $_REQUEST['StartMonth'];
 $startyear = $_REQUEST['StartYear'];
 $endmonth = $_REQUEST['EndMonth'];
 $endyear = $_REQUEST['EndYear'];
 $message = $_REQUEST['Message'];

 $fields = array(); 
 $fields{"Name"} = "Name"; 
 $fields{"Email"} = "Email"; 
 $fields{"Phone"} = "Phone"; 

 $selectedProjects  = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
    $selectedProjects = implode(', ', $_POST['projects']);
}
 $selectedSkills  = 'None';
if(isset($_POST['skills']) && is_array($_POST['skills']) && count($_POST['skills']) > 0){
    $selectedSkills = implode(', ', $_POST['skills']);
}
$selectedNoRush  = 'None';
if(isset($_POST['norush']) && is_array($_POST['norush']) && count($_POST['norush']) > 0){
    $NoRush= implode(', ', $_POST['norush']);
}
$selectedWhenReady  = 'None';
if(isset($_POST['whenready']) && is_array($_POST['whenready']) && count($_POST['whenready']) > 0){
    $WhenReady= implode(', ', $_POST['whenready']);
}
$selectedBudget  = 'None';
if(isset($_POST['budget']) && is_array($_POST['budget']) && count($_POST['budget']) > 0){
    $selectedBudget= implode(', ', $_POST['budget']);
} 

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);}
$body .= "\n" . 'Selected Projects: ' . $selectedProjects . "\n";
$body .= 'Selected Skills: ' . $selectedSkills . "\n\n";
$body .= 'Start Date: ' . $startmonth . " " . $startyear . " " . $NoRush . "\n";
$body .= 'End Date: ' . $endmonth . " " . $endyear . " " . $WhenReady . "\n";
$body .= 'Budget: ' . $selectedBudget . "\n\n";
$body .= 'Message:' . $message . "\n";



 $headers2 = "From: me@gmail.com"; 
 $subject2 = "Thank you for contacting us"; 
 $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours.";

 if($from == '') {print "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {print "You have not entered a name, please go back and try again";} 
 else {
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send) 
 {print "Thank you. Your request has been successfully submitted.";} 
 else 
 {print "We encountered an error sending your mail, please check your details are correct or email us at hello@lyonempire.co.uk"; } 
 }
}
 ?>

所以上面一切正常,但是当我在名称/电子邮件检查后添加以下代码时,它会中断:

if($message == '') {print "You have not entered a message, please go back and try again";} 
 else {

我做错了什么?

谢谢! MC

2 个答案:

答案 0 :(得分:1)

if($name == '')
{
    print "You have not entered a name, please go back and try again";
}
else if($message == '') {
    // do what ever you want
} 
else {
    $send = mail($to, $subject, $body, $headers); 
    $send2 = mail($from, $subject2, $autoreply, $headers2); 

....休息到这里

答案 1 :(得分:0)

我无法从您展示的代码中得知,但您是否已关闭了其他声明中的大括号?

if($message == '') {print "You have not entered a message, please go back and try again";} 
else {} <----