-3

I have this PHP code:

$htmlCode = '
                <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                </form>
            ';

And I want to put it in a part of my HTML using this:

<html>
   <?php
       echo $htmlCode;
   ?>
</html>

But when I open the file it shows this: enter image description here

And I don't know why. Please help

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24

2 Answers2

0

Your PHP-Code should look like:

<?php
$htmlCode = '
            <form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'"     method="post">
            </form>
        ';

?>

gdinnebier
  • 51
  • 3
0

this is big mistake

try this code


<?php

$path = htmlspecialchars($_SERVER["PHP_SELF"]);
$htmlCode = "<form action='{$path}' method='post'></form>";
echo $htmlCode;

even you don't need $path this is also work

<?php
$htmlCode = '<form action"" method="post"></form>';
echo $htmlCode;

?>

mohammad13
  • 453
  • 3
  • 17
  • 1
    You don't even need to echo the `$htmlCode`. Just add the `
    ` tag into the HTML body. No PHP is required.
    – Raptor May 07 '21 at 09:15