【问题标题】:Syntax error unexpected '<' [duplicate]语法错误意外'<' [重复]
【发布时间】:2015-12-22 22:07:29
【问题描述】:

对于结束标记所在的行,我不断收到此语法错误,我终生无法弄清楚我做错了什么。我以为我按照教科书的说明完成了这项作业,但如果我遇到错误,我显然没有。我一定是盲目的,因为我无法发现错误。任何帮助将不胜感激。

  <!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>

【问题讨论】:

  • 突出显示出现此错误的行?
  • 对于初学者,第二个“
  • 发现错误的一个好方法是从代码中删除一些位,直到问题被隔离 - 您可以将噪音降低到可以清楚地看到问题的程度,或者删除的块可以解决问题,您可以孤立地看待它 - 如果您仍处于有大量代码块的阶段,那么您还没有这样做,我们当然不会为您这样做
  • @arch 没必要这么讨厌,即使是非常有经验的程序员也很难看到简单的语法错误,这就是为什么像任何专业程序员一样,重新审视某事通常会很快解决它知道
  • 您在&lt;/body&gt; 之前缺少} ?&gt;,所以只需在&lt;/body&gt; 之前添加} ?&gt; 并检查。这是一个愚蠢的错误。

标签: php html forms


【解决方案1】:

你没有关闭最后一个 else

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";
} ?>

【讨论】:

  • 嗯,谢谢,这似乎消除了错误。奇怪的是,我的教科书没有包括结尾?> 因为我是直接抄袭的,所以我没有注意到,甚至没有想过。
猜你喜欢
  • 2016-04-11
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 2018-06-28
  • 2016-02-07
  • 1970-01-01
  • 2013-12-09
相关资源
最近更新 更多