-2

How do you call an input tag in a here document when that here document is in a variable in php?

Here is my code:

<?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'];
?>

I've tried using 'get' instead of 'post' but that didn't work. I also tried replacing the '$_POST' to '$myForm', that didn't work either.your text

  • 3
    `$_POST` won't be set until the user submits the form the next time. You can't use it when you're creating the form (unless you want the value from the previous submission). – Barmar Apr 24 '23 at 19:41
  • 3
    What _exactly_ is the issue you're facing here? The main problem I can see is that $numRows = $_POST['numRows'] is going to be executed _before_ the user has actually submitted the form. That has nothing to do with the usage of heredoc – ADyson Apr 24 '23 at 19:42
  • I am trying to get the input from the user from the text input. – Anya Dinger Apr 24 '23 at 19:45
  • 1
    Does this answer your question? [Checking if form has been submitted - PHP](https://stackoverflow.com/questions/7711466/checking-if-form-has-been-submitted-php) – ADyson Apr 24 '23 at 19:51
  • 1
    On a side note, heredoc does not seem to be the most convenient syntax to inject individual variables in large chunks of HTML. Look at https://www.php.net/manual/en/tutorial.firstpage.php for something more convenient. – Álvaro González Apr 25 '23 at 14:37

1 Answers1

1

There's a slight change in the way array string indexes are used inside heredoc. Also, you are missing the POST method check for conditionally handing the processing

<?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;
}
?>
Deepak Thomas
  • 3,355
  • 4
  • 37
  • 39