-14

I keep getting this syntax error for the line where the closing tag is and I can not for the life of me figure out what I did wrong. I thought I followed the instructions for my textbook for this assignment but I obviously didn't if I am getting an error. I must be blind because I can't spot the error. Any help would be greatly appreciated.

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Contact Me</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
function validateInput($data, $fieldName) {
    global $errorCount;
    if (empty($data)) {
        echo "\"$fieldName\" is a required field. <br />\n";
        ++$errorCount;
        $retval = "";
    } else { //Only clean up the input if it isn't // empty
    $retval = trim($data);
    $retval = stripslashes($retval);
    }
    return($retval);
}
function validateEmail ($data, $fieldName) {
    global $errorCount;
    if (empty($data)) {
        echo "\"$fieldName\" is a required field.<br />\n";
        ++$errorCount;
        $retval = "";
    } else { // Only Clean up the input if it isn't // empty
    $retval = trim($data);
    $retval = stripslashes($retval);
    $pattern = "/^[\w-]+(\.[\w-]+)*@" .
    "[\w-]+(\.[\w-]+)*" .
    "(\. [[a-z]]{2,})$/i";
    if (preg_match($pattern, $retval)==0) {
        echo "\"$fieldName\" is not a valid e-mail address.<br />\n";
        ++$errorCount;
    }
    }
    return($retval);
}
function displayForm($Sender, $Email, $Subject, $Message) {
?>
<h2 style= "text-align:center">Contact Me</h2>
<form name="contact" action="ContactForm.php" method="post">
<p>Your Name: </p>
<p>Your E-mail: 
  <input type="text" name="Sender" value="<?php
echo $Sender; ?>" />
  <input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Subect: <input type="text" name="Subject" value="<?php echo $Subject; ?>" /></p>
<p>Message:<br /> 
</p>
<p><input type="reset" value="Clear Form" />&nbsp; &nbsp;<input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$ShowForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";
if (isset($_POST['Submit'])) {
    $Sender = 
    validateInput($_POST['Sender'], "Your Name");
    $Email = 
    validateEmail($_POST['Email'], "Your E-mail");
    $Subject = 
    validateInput($_POST['Subject'], "Subject");
    $Message = 
    validateInput($_POST['Message'],"Message");
    if ($errorCount==0)
    $ShowForm = FALSE;
    else
    $ShowForm = TRUE;
}
if ($ShowForm == TRUE) {
    if ($errorCount>0) // if there were errors echo "<p>Please re-enter the form information below.</p>\n"; 
    displayForm($Sender, $Email, $Subject, $Message);
}
else {
    $SenderAddress= "$Sender <$Email>";
    $Headers= "From: $SenderAddress\nCC:
    $SenderAddress\n";
    // Substitute your own email address for // recipient@example.com
    $result = mail ("recipient@example.com",
    $Subject, $Message, $Headers);
    if ($result)
    echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
    else
    echo "<p>There was an error sending your message, " .
    $Sender . ".</p>\n";
</body>
</html>
  • 1
    highlight the line where you are getting this error? – Happy Coding Sep 25 '15 at 06:24
  • 1
    For starters, the second " – DrWatson Sep 25 '15 at 06:25
  • a good way of spotting an error is to remove bits from the code until the problem is isolated - you either reduce the noise to a point where you can clearly see the problem, or a chunk you remove solves the problem and you can look at that in isolation - if you're still at the stage where there's a huge block of code then you haven't done that and we certainly aren't going to do it for you – Toni Leigh Sep 25 '15 at 06:30
  • @arch there's no need to be quite so nasty, even very experienced programmers struggle to see simple syntax errors, that's why getting a second set of eyes on something often solves it very quickly, as any professional programmer knows – Toni Leigh Sep 25 '15 at 06:31
  • You missing `} ?>` before `

    `, So just add `} ?>` before `` and check. this is so silly mistake.

    – Kausha Mehta Sep 25 '15 at 06:33
  • Close else block and php tag ?>... – Shashank Singh Sep 25 '15 at 06:33

1 Answers1

0

You are not closing the last else block

else {
    $SenderAddress= "$Sender <$Email>";
    $Headers= "From: $SenderAddress\nCC:
    $SenderAddress\n";
    // Substitute your own email address for // recipient@example.com
    $result = mail ("recipient@example.com",
    $Subject, $Message, $Headers);
    if ($result)
    echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
    else
    echo "<p>There was an error sending your message, " .
    $Sender . ".</p>\n";
} ?>
Harshit
  • 5,147
  • 9
  • 46
  • 93
  • Hmm thank you, that seemed to get rid of the error. Strangely my textbook didn't include the closing ?> and since I was copying straight from it, I did not notice or even think about that. – user5374923 Sep 25 '15 at 06:35