0

I'm new in PHP. I have simple script which displays my string variable ('Hello, World') when I press the button. But it doesn't work if I put the form into a loop. I guess value of hidden input didn't initialize by some reason. How can I fix it?

<body>
<?php 
    $testvariable = 'Hello, World!';
    
    for ($i=0; $i < 3; $i++) { 
        echo '<form name="tesst" method="POST" action="test.php">
                <input type="hidden" name="hidden_name" value="<?php echo $testvariable ?>">
                <input type="submit" name="te1st" value="Buttonfor">
              </form>';
    }
?>
<form name="tesst" method="POST" action="test.php">
        <input type="hidden" name="hidden_name" value="<?php echo $testvariable ?>">
        <input type="submit" name="te1st" value="Button1">
</form>
<?php
    if (isset($_POST) && isset($_POST['hidden_name']))
        echo strval($_POST['hidden_name']);
?>
[Result][https://i.stack.imgur.com/mrs6R.png]
Nurtugang
  • 13
  • 5
  • You're already building a HTML string in your `echo` expression. You can't nest `` inside it. I recommend using the [alternative syntax for control structures](https://www.php.net/manual/control-structures.alternative-syntax.php) instead of creating HTML strings – Phil Apr 10 '22 at 06:20
  • Thanks, I'm gonna test it now UPD: IT WORKS – Nurtugang Apr 10 '22 at 06:21

0 Answers0