2013-06-26 74 views
-2

我有一個在聯繫表單頁面上執行的PHP表單。我已經得到它的工作 - 但它目前只進行檢查,以確保名稱和電子郵件進入。我試圖讓它也檢查一個消息,但我的嘗試只是cos頁面不加載後的PHP。下面是我有:PHP POST表單驗證錯誤

<?php 
$to = "[email protected]" ; 
$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: [email protected]"; 
$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 [email protected]"; } 
} 
} 
?> 

所以上面的一切工作正常,但是當我的名字/電子郵件檢查後添加以下代碼,它打破:

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

我在做什麼錯?

謝謝! MC

+0

這是最不尋常的包圍式的,我最近看到... –

+0

'它breaks'。它以什麼方式打破?你收到什麼信息?你的代碼看起來是如何與包含的行(只顯示前後兩行,而不是隻有新行) – Arjan

+0

@rr這是我第一次去PHP代碼 - 請讓我知道爲什麼它是不尋常的,因爲我正在拼湊在一起,這將真正有助於知道... – Mike

回答

0

我不能告訴你已經顯示的代碼,但是你是否關閉了else語句中的大括號?

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

就是這樣,我覺得很愚蠢。 – Mike

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); 

....此處添加其餘