提问人:Anya Dinger 提问时间:4/25/2023 更新时间:4/25/2023 访问量:62
PHP中的Here Document
Here Document in PHP
问:
当 here 文档位于 php 的变量中时,如何调用 here 文档中的输入标签?
这是我的代码:
<?php
$myForm = <<<FORMSTUFF
<h3>Generate ASCII Table</h3>
<form action="$_SERVER[PHP_SELF]" method="post">
<label for="numRows">Number of rows:</label>
<input type="text" name="numRows" id="numRows" value="32">
<br><br>
<input type="submit" name="go" value="Create ASCII Table">
</form>
FORMSTUFF;
$numRows = $_POST['numRows'];
?>
我尝试使用“get”而不是“post”,但这没有用。我还尝试将“$_POST”替换为“$myForm”,但也没有用。your text
答:
1赞
Deepak Thomas
4/25/2023
#1
在 heredoc 中使用数组字符串索引的方式略有变化。此外,您还缺少用于有条件地处理处理的 POST 方法检查
<?php
$myForm = <<<FORMSTUFF
<h3>Generate ASCII Table</h3>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<label for="numRows">Number of rows:</label>
<input type="text" name="numRows" id="numRows" value="32">
<br><br>
<input type="submit" name="go" value="Create ASCII Table">
</form>
FORMSTUFF;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$numRows = $_POST['numRows'];
// your code comes here;
return;
}
?>
评论
0赞
Deepak Thomas
4/25/2023
使用开机自检检查更新了答案
评论
$_POST
在用户下次提交表单之前不会设置。在创建表单时,不能使用它(除非需要上一次提交的值)。