-1

For some reason I keep getting an error within my backend code for my contact form

Here is my contact form

  <div id = "form">

<form action ="contact2.php" method="post">
Hi Rebekah My Name Is
<br>
<input type="text" name="name">
<br>
My Email Is
<br>
<input type="text" name="email">
<br>
My Message Is
<br>
<TEXTAREA NAME="message" ROWS=6 COLS=40>
</TEXTAREA>

<br>

<input type="submit" value="Submit">

<?php 
include "contact2.php";
echo $result; 

?>

</div> 

Here is my backend PHP code

<?php

$field_name = $_POST['name'];
$email = $_POST['email'];
$field_message = $_POST['message'];

$mail_to = 'example@yahoo.com';

$subject = 'Message from a site visitor ' . $field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$field_message;

$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);


// Check if name has been entered
if (!$_POST['name']) {
    
$field_name = 'Please enter your name';
}
 
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $email = 'Please enter a valid email address';
}
 
//Check if message has been entered
if (!$_POST['message']) {
    $field_message = 'Please enter your message';
}

// If there are no errors, send the email
if (!$field_name && !$email && !$field_message) {
    if (mail ($_to, $subject, $body_message, $headers)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    } 
?>

When I run the code there is said to be an error in my php code on line 43 which is the last line but I can't seem to see what the problem is as this is only the closing tag for the php code, when I take the closing php tag out I still get the same message.

Here is the message that is displayed.

Parse error: syntax error, unexpected end of file in D:\contact2.php on line 43

I have taken out where the file is found from the above for safety reasons.

Rebekah
  • 21
  • 4

3 Answers3

1

Your if (!$field_name && !$email && !$field_message) { is not closed.

staskus
  • 171
  • 8
1

You are missing the closing tag of your if statement.

Add } before your closing tag ?> on the last line.

Albzi
  • 15,431
  • 6
  • 46
  • 63
Huso
  • 1,501
  • 9
  • 14
1

Your last IF statement is lacking a }

if (!$field_name && !$email && !$field_message) {
    if (mail ($_to, $subject, $body_message, $headers)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
} // <-----
bytecode77
  • 14,163
  • 30
  • 110
  • 141