这里是 PHP 文档

问题描述 投票:0回答:1

当 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

php heredoc
1个回答
0
投票

在 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;
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.