如何在 echo 命令中执行 $_SERVER['PHP_SELF']


How can you do a $_SERVER['PHP_SELF'] inside an echo command?

我真的以为我把这个放下了,但由于某种原因,在回声中它不想执行命令。 它只是将其回显到网页网址。

http://localhost/<?php echo htmlentities($_SERVER[''PHP_SELF'']); ?>
<?php 
  echo' <form name="action" action="<?php echo htmlentities($_SERVER[''PHP_SELF'']); ?>" method="post">
         <input type="text" name="name"><br>
         <input type="submit" name="submit" value="Submit Form"><br>
    </form>';
?>

我打算尝试将其发布给自己,然后如果有发布数据,那么:

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])){ 
  //do something here
}

我也试图打破回声:

echo "  <form name='action' action=".<?php echo htmlentities($_SERVER['PHP_SELF']); ?>." method="post">
         <input type='text' name='name'><br>
         <input type='submit' name='submit' value='Submit Form'><br>
       </form>";

你不能在另一个 php/echo 语句中做一个 echo 语句。只有当你已经脱离了 php 时,你才应该这样做:

echo "  <form name='action' action=".htmlentities($_SERVER['PHP_SELF'])." method="post">
     <input type='text' name='name'><br>
     <input type='submit' name='submit' value='Submit Form'><br>
   </form>";

或在 PHP 之外:

?>
<form name='action' action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type='text' name='name'><br>
<input type='submit' name='submit' value='Submit Form'><br>
</form>
<?php