[英]PHP Form Confirmation Parse Error
我有一些非常簡單的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"> <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
我不確定自己在做什么錯,盡管我可能已經忘記了分號。
PHP解析器對此感到困惑:
$ message =“;
用。。。來代替
$ message =“”;
除了錯誤$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>
你在這:
$message = ";
也許是:
$message = "";
另外,您不需要以這種方式聲明它,您還可以:
$message;
您唯一的錯誤是您已將要讓php打印的文本加上雙引號,這意味着php將對其進行評估,並查看是否需要使用它進行處理。 相反,您應該將其放在單引號中,如下所示:...':'...
用單引號引起的任何內容將按字符原樣打印。 任何雙引號都會被評估。
我認為可能是
$message = "";
foreach ($_POST['somevalue'] as $key => $value) {
$message .= $key . ":" .$value. "<br>\r\n";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.