2012-03-21 77 views
3

我正在創建一個基本的聯繫表單,其中包含一些必填字段和從下拉菜單中進行的必需選擇。填充字段工作正常,但下拉菜單選擇要求導致解析錯誤。獲取有關所需的基本聯繫表格中的下拉菜單項選擇的PHP解析錯誤

我注意到下拉菜單要求的任何實例都發現錯誤消失了。所以這個錯誤與下拉菜單選擇有關。根據錯誤日誌,問題在第49行。我嘗試了幾次重寫該行,但沒有取得太大的成功。

錯誤是由第49行中的某些內容引起的,還是在我的語法中的其他地方?

這是我第一次寫PHP,所以非常感謝任何幫助。

<?php 
if(isset($_POST['email'])) { 

// EMAIL and SUBJECT 
$email_to = "[email protected]"; 

$email_subject = "Test Form Dev"; 


function died($error) { 
    // ERROR CODE 
    echo "We apologize for the inconvenience, but there were error(s) found with your form submission. "; 
    echo "These errors appear below.<br /><br />"; 
    echo $error."<br /><br />"; 
    echo "Please go back and correct the error(s).<br /><br />"; 
    die(); 
} 

// VALIDATION EXPECTED DATA EXISTS 
if(!isset($_POST['first_name']) || 
    !isset($_POST['last_name']) || 
    !isset($_POST['email']) || 
    !isset($_POST['telephone']) || 
    !isset($_POST['inquiry']) || 
    !isset($_POST['comments'])) { 
    died('We are sorry, but there appears to be a problem with the form you submitted.');  
} 

$first_name = $_POST['first_name']; // REQUIRED 
$last_name = $_POST['last_name']; // REQUIRED 
$email_from = $_POST['email']; // REQUIRED 
$telephone = $_POST['telephone']; // NOT REQUIRED 
$inquiry_type = $_POST['inquiry']; // REQUIRED 
$comments = $_POST['comments']; // REQUIRED 

$error_message = ""; 
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email_from)) { 
$error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
} 
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$first_name)) { 
$error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
} 
if(!preg_match($string_exp,$last_name)) { 
$error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
} 
$inquiry_exp = 'Charter, Media, Broker,'; // drop-down menu options 
if(strlen($inquiry) < 1) { 
$error_message .= 'Please select inquiry type.<br />'; 
} 
if(strlen($comments) < 2) { 
$error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
} 
if(strlen($error_message) > 0) { 
died($error_message); 
} 
$email_message = "Form details below.\n\n"; 

function clean_string($string) { 
    $bad = array("content-type","bcc:","to:","cc:","href"); 
    return str_replace($bad,"",$string); 
} 

$email_message .= "First Name: ".clean_string($first_name)."\n"; 
$email_message .= "Last Name: ".clean_string($last_name)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Telephone: ".clean_string($telephone)."\n"; 
$email_message .= "Inquiry Type: ".clean_string($inquiry)."\n"; 
$email_message .= "Comments: ".clean_string($comments)."\n"; 


// CREATE EMAIL HEADERS 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- RETURN MESSAGE (HTML): SUCCESSFUL FORM SUBMISSION --> 

<p>Thank-you message goes here.</p> 

<?php 
} 
die(); 
?> 

編輯:我正在MAMP環境中構建此表單。我在其他地方看過我需要創建一個htaccess文件,但對於本地開發人員來說這是必需的嗎?編輯2:在環顧其他論壇之後,我瞭解到我必須在PHP中單獨分解下拉菜單項。我完成了,但仍然得到Parse:第98行(最後一行)的語法錯誤,聲明「意外的$結束」。但是,我無法選擇菜單選項來填充生成的電子郵件,也無法找出導致錯誤的具體原因。我最初根據錯誤日誌修復了我的代碼,但沒有成功。

這裏是我更新的代碼:

<?php 
if(isset($_POST['email'])) { 

// EMAIL and SUBJECT 
$email_to = "[email protected]"; 

$email_subject = "XXX"; 


function died($error) { 
    // ERROR CODE 
    echo "We apologize for the inconvenience, but there were error(s) found with your form submission. "; 
    echo "These errors appear below.<br /><br />"; 
    echo $error."<br /><br />"; 
    echo "Please go back and correct the error(s).<br /><br />"; 
    die(); 
} 

// VALIDATION EXPECTED DATA EXISTS 
if(!isset($_POST['first_name']) || 
    !isset($_POST['last_name']) || 
    !isset($_POST['email']) || 
    !isset($_POST['telephone']) || 
    !isset($_POST['inquiry']) || 
    !isset($_POST['comments'])) { 
    died('We are sorry, but there appears to be a problem with the form you submitted.');  

$first_name = $_POST['first_name']; // REQUIRED 
$last_name = $_POST['last_name']; // REQUIRED 
$email_from = $_POST['email']; // REQUIRED 
$telephone = $_POST['telephone']; // NOT REQUIRED 
$comments = $_POST['comments']; // REQUIRED 
$inquiry = $_POST['inquiry']; 

if(empty($inquiry) || $inquiry == "null") 
    // If there isn't a value for the dropdown, or they've selected the option 
    // that reads "Please select one" then return an error 
    die("Please select your reason for inquiring on the drop-down menu."); 

switch($inquiry){ 

    case "Broker" : die(); break; 
    case "Press" : die(); break; 
    case "Charter" : die(); break; 
    default : die(); 

} 

} 

$error_message = ""; 
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email_from)) { 
$error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
} 
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$first_name)) { 
$error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
} 
if(!preg_match($string_exp,$last_name)) { 
$error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
} 
if(strlen($comments) < 2) { 
$error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
} 
if(strlen($error_message) > 0) { 
died($error_message); 
} 
$email_message = "Form details below.\n\n"; 

function clean_string($string) { 
    $bad = array("content-type","bcc:","to:","cc:","href"); 
    return str_replace($bad,"",$string); 
} 

$email_message .= "First Name: ".clean_string($first_name)."\n"; 
$email_message .= "Last Name: ".clean_string($last_name)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Telephone: ".clean_string($telephone)."\n"; 
$email_message .= "Inquiry Type: ".clean_string($inquiry)."\n"; 
$email_message .= "Comments: ".clean_string($comments)."\n"; 


// CREATE EMAIL HEADERS 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- place your own success html below --> 

Thank you for contacting us. We will be in touch with you very soon. 

<?php 
} 
die(); 
?> 

任何人都可以提供任何見解是什麼引起的形式出錯了呢?

+0

哪一行是第49行?錯誤是什麼? – Cfreak 2012-03-21 16:48:06

+0

49行是哪一個..? – 2012-03-21 16:48:21

+0

此代碼沒有語法錯誤 – 2012-03-21 16:50:05

回答

1

上線49變化if(strlen($inquiry) < 1){ ...if(strlen($inquiry_type) < 1) 也改變clean_string($inquiry)clean_string($inquiry_type)上線69

+0

我糾正了不匹配的標記並使它們全部一致,但仍然出現錯誤。這可能是由現場文本/符號要求造成的嗎? – Caroline 2012-03-21 17:06:41

1

您還沒有宣佈$inquiry變量,因此下面的行會報告錯誤:

if(strlen($inquiry) < 1) { 
$email_message .= "Inquiry Type: ".clean_string($inquiry)."\n"; 

你有一個$inquiry_type變量,所以這可能是一個錯字。

+0

感謝您收到錯字。我在整個代碼中使變量保持一致並重新測試,但仍然出現錯誤。我檢查了錯誤日誌,但沒有看到任何東西(最後記錄的錯誤來自昨天)。 我在想這個錯誤可能是由字段文本/符號要求造成的? – Caroline 2012-03-21 17:02:50