2012-03-21 82 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