douzinei6926 2012-03-21 16:45
浏览 50

在基本联系表单中获取有关所需下拉菜单项选择的PHP解析错误

I'm creating a basic contact form with a few required fields and a required selection from a drop-down menu. The fill-in fields are working correctly, however the drop-down menu selection requirement is causing a parse error.

I commented out any instances of the drop-down menu requirement to find that the error is gone. So the error has something to do with the drop-down menu selection. According to the error logs, the problem is in line 49. I tried rewriting that line a few times without much success.

Is the error caused by something specifically in line 49 or is elsewhere in my syntax?

This is my first time writing PHP, so any help is greatly appreciated.

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

// EMAIL and SUBJECT
$email_to = "xxx@xxx.com";

$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._%-]+@[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.

";

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)."
";
$email_message .= "Last Name: ".clean_string($last_name)."
";
$email_message .= "Email: ".clean_string($email_from)."
";
$email_message .= "Telephone: ".clean_string($telephone)."
";
$email_message .= "Inquiry Type: ".clean_string($inquiry)."
";
$email_message .= "Comments: ".clean_string($comments)."
";


// CREATE EMAIL HEADERS
$headers = 'From: '.$email_from."
".
'Reply-To: '.$email_from."
" .
'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();
?>

Edit: I'm building this form in a MAMP environment. I've read elsewhere that I need to create an htaccess file, but is that necessary for a local dev?

Edit 2: After looking around other forums, I learned that I have to break down the dropdown menu items individually in PHP. I got that accomplished, but am still getting Parse:syntax errors on line 98 (the last line) stating "unexpected $end". However, I cannot get the menu selection to populate in the generated email nor figure out what specifically is causing the error. I originally fixed my code according to the error log without success.

Here's my updated code:

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

// EMAIL and SUBJECT
$email_to = "xxx@xxx.com";

$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._%-]+@[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.

";

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)."
";
$email_message .= "Last Name: ".clean_string($last_name)."
";
$email_message .= "Email: ".clean_string($email_from)."
";
$email_message .= "Telephone: ".clean_string($telephone)."
";
$email_message .= "Inquiry Type: ".clean_string($inquiry)."
";
$email_message .= "Comments: ".clean_string($comments)."
";


// CREATE EMAIL HEADERS
$headers = 'From: '.$email_from."
".
'Reply-To: '.$email_from."
" .
'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();
?>

Can anyone provide any insight to what's causing the form to error out?

  • 写回答

2条回答 默认 最新

  • dongzhanlu8890 2012-03-21 16:50
    关注

    CHange if(strlen($inquiry) < 1){ ... on line 49 to if(strlen($inquiry_type) < 1) Also change clean_string($inquiry) to clean_string($inquiry_type) on line 69

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用