3

いくつかの必須フィールドとドロップダウン メニューからの必須選択を含む基本的な連絡フォームを作成しています。入力フィールドは正しく機能していますが、ドロップダウン メニューの選択要件により解析エラーが発生しています。

エラーがなくなったことを確認するために、ドロップダウン メニュー要件のインスタンスをすべてコメントアウトしました。したがって、エラーはドロップダウン メニューの選択に関係しています。エラー ログによると、問題は 49 行目にあります。その行を数回書き直そうとしましたが、あまり成功しませんでした。

エラーの原因は 49 行目ですか、それとも構文のどこかにありますか?

PHPを書くのはこれが初めてなので、助けていただければ幸いです。

<?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.\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 でドロップダウン メニュー項目を個別に分割する必要があることを知りました。私はそれを達成しましたが、まだ 98 行目 (最後の行) で "unexpected $end" という Parse:syntax エラーが発生しています。ただし、生成された電子メールにメニュー選択を入力することも、エラーの具体的な原因を特定することもできません。エラーログに従ってコードを修正しましたが、成功しませんでした。

更新されたコードは次のとおりです。

<?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.\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();
?>

フォームのエラーの原因について誰か洞察を提供できますか?

4

2 に答える 2

1

49if(strlen($inquiry) < 1){ ...行目を変更69 行目if(strlen($inquiry_type) < 1) も変更clean_string($inquiry)clean_string($inquiry_type)

于 2012-03-21T16:50:49.547 に答える
1

変数を宣言していない$inquiryため、次の行でエラーが報告されます。

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

$inquiry_type変数があるので、これはおそらくタイプミスです。

于 2012-03-21T16:51:46.710 に答える