Getting PHP parsing error regarding selected dropdown menu selection in basic contact form

I am creating a basic contact form with a few required fields and a required selection from a dropdown menu. The padding fields work correctly, however, the requirement to select a dropdown menu causes a parsing error.

I commented out any dropdown menu examples to see if the error is gone. So the error has something to do with the dropdown. According to the error logs, the problem is on line 49. I tried rewriting this line several times without much success.

Is the error causing something specifically on line 49 or 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.\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();
?>

      

Edit: I am creating this form in a MAMP environment. I read elsewhere that I need to create an htaccess file, but is that necessary for a local developer?

Edit 2: After looking at other forums, I found out that I have to split individual menu items in PHP. I got this, but I still get Parse syntax errors: line at line 98 (last line) with "unexpected end of $". However, I am unable to get the menu selection to populate in the generated email, or figure out what exactly is causing the error. I originally corrected my code to match the error log.

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

      

Can anyone provide an idea of ​​what is causing the form error?

+3


source to share


2 answers


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



+1


source


You have not specified a variable $inquiry

, so the following lines will report errors:

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

      



You have a variable $inquiry_type

, so this is probably a typo.

+1


source







All Articles