简体   繁体   English

PHP表单确认解析错误

[英]PHP Form Confirmation Parse Error

I have some very simple PHP code that I'm using to confirm input from a form. 我有一些非常简单的PHP代码,用于确认表单中的输入。

HTML: 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

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

The PHP code keeps producing an error. PHP代码不断产生错误。

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

I'm not sure what I'm doing wrong, although I feel I might have forgotten a semicolon. 我不确定自己在做什么错,尽管我可能已经忘记了分号。

the PHP parser get confused by this: PHP解析器对此感到困惑:

$message = "; $ message =“;

replace with 用。。。来代替

$message = ""; $ message =“”;

Apart from error $message = "; to $message = ""; You are using print inside print to show message( print $message ). 除了错误$message = ";$message = "";您还使用print里面的print显示消息( print $message )。

Alternate Solution :- 替代解决方案:-

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

There you are: 你在这:

$message = ";

Maybe it's : 也许是:

$message = "";

Also, you don't need to declare it in this way, you can also: 另外,您不需要以这种方式声明它,您还可以:

$message;

your only mistake is that you have put the text thjat you wanted php to print in double quotes, this means that php will evaluate it and see if it needs to process something with it. 您唯一的错误是您已将要让php打印的文本加上双引号,这意味着php将对其进行评估,并查看是否需要使用它进行处理。 Instead you should have put it in single quote like this: ... ':' ... 相反,您应该将其放在单引号中,如下所示:...':'...

Anything quoted in single quotes will be printed as-it-is, character by character. 用单引号引起的任何内容将按字符原样打印。 Anything in double quotes will be evaluated. 任何双引号都会被评估。

I think it could be 我认为可能是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM