0

我有一些非常简单的 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

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

4

5 回答 5

3

PHP解析器对此感到困惑:

$消息 = ";

用。。。来代替

$消息 = "";

于 2013-12-12T09:36:56.720 回答
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>
于 2013-12-12T09:38:12.300 回答
0

你在这:

$message = ";

也许是:

$message = "";

另外,您不需要以这种方式声明它,您还可以:

$message;
于 2013-12-12T09:37:04.670 回答
0

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

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

于 2013-12-12T09:43:43.093 回答
0

我想可能是

$message = "";
foreach ($_POST['somevalue'] as $key => $value) {
    $message .= $key . ":" .$value. "<br>\r\n";
}
于 2013-12-12T09:47:45.297 回答