【问题标题】:PHP Form Confirmation Parse ErrorPHP表单确认解析错误
【发布时间】:2013-12-30 15:19:00
【问题描述】:

我有一些非常简单的 PHP 代码,用于确认来自表单的输入。

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Unit 7 - Homework 5</title>
    </head>
    <body>
        <h1>Contact us</h1>
        <table>
            <form method="POST" action="script.php">
                <tr>
                    <td>First Name:</td> 
                    <td><input type="text" name="First Name" id="fname"></td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td><input type="text" name="Last Name" id="lname"></td>
                </tr>
                <tr>
                    <td>E-mail:</td>
                    <td><input type="text" name="E-mail" id="email"></td>
                </tr>
                <tr>
                    <td>Comments</td>
                    <td><textarea rows="10" cols="40"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Contact"> &nbsp; <input type="reset"></td>
                </tr>
            </form>
        </table>
    </body>
</html>

PHP

<?php print "<!DOCTYPE html>
<html lang=\"en\">
<head>
    <title>Form Confirmation</title>
</head>
<body>
    <h1>Congratulations, registration done!</h1>";
    $message = ";
    foreach ($_POST as $key => $value) {
        $message .= $key . ":" .$value. "<br>\r\n";
    }

    print $message;
    print "<br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <form action=\"#\">
    <input type=\"button\" value=\"Back\" onclick=\"javascript:history.go(-1)\" />
    </form>
</body>
</html>"
?>

PHP 代码不断产生错误。

Parse error: syntax error, unexpected ':' in <PHP path goes here> on line 10

我不确定自己做错了什么,尽管我觉得我可能忘记了分号。

【问题讨论】:

  • 看,这里的语法高亮显示了错误的确切位置。 -1 因为语法错误很少是 stackoverflow 的好问题:大多数时候,充其量,它们过于本地化,不能成为我们社区的好问题。

标签: php forms post confirmation php-parse-error


【解决方案1】:

除了错误$message = "; 到$message = ""; 你在print 中使用print 来显示消息(print $message)。

替代解决方案:-

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Form Confirmation</title>
</head>
<body>
    <h1>Congratulations, registration done!</h1>
    <?php

    $message = '';
    foreach ($_POST as $key => $value) {
        $message .= $key . ":" .$value. "<br>\r\n";
    }

    print $message;
    ?>
    <form action="#">
    <input type="button" value="Back" onclick="javascript:history.go(-1)" />
    </form>
</body>
</html>

【讨论】:

  • 等等,我可以将 PHP 代码添加到 HTML 中并且只有一个文件而不是两个文件吗?我现在 PHP 是一种服务器端脚本语言,但由于这只是一个家庭作业,目标是在本地执行它。
  • 不,您不能将 php 代码添加到 HTML 中。它甚至不会解析代码。所有 php 代码都将显示在您的网页上而不会出现任何错误。
【解决方案2】:

我觉得可以

$message = "";
foreach ($_POST['somevalue'] as $key => $value) {
    $message .= $key . ":" .$value. "<br>\r\n";
}

【讨论】:

    【解决方案3】:

    你唯一的错误是你把你想要 php 打印的文本放在双引号中,这意味着 php 将评估它并查看它是否需要处理它。相反,您应该将其放在单引号中,如下所示: ... ':' ...

    单引号中引用的任何内容都将按原样逐字符打印。 将评估双引号中的任何内容。

    【讨论】:

      【解决方案4】:

      你来了:

      $message = ";
      

      也许是:

      $message = "";
      

      另外,你不需要这样声明,你也可以:

      $message;
      

      【讨论】:

        【解决方案5】:

        PHP 解析器对此感到困惑:

        $message = ";

        替换为

        $message = "";

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-30
          • 2021-09-17
          • 1970-01-01
          • 2012-04-20
          • 2014-09-27
          • 1970-01-01
          相关资源
          最近更新 更多