0

I really thought I had this one down, but for some reason inside the echo it doesn't want to perform the command. It just echo's it to the web page url.

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

I was going to try and have it post to self and then if there is post data then:

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

I tried to break the echo also:

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>";

1 Answers1

0

You can't do an echo statement inside of another php/echo statement. You should only do that if you've broken out of 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>";

or outside of 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
aynber
  • 22,380
  • 8
  • 50
  • 63
  • I was doing an If {} Else{} with php, so how can I make this work? Would I have to ?> echo... –  Jul 15 '16 at 15:41
  • 1
    I updated my answer with how you can do it outside of PHP. So you can surround the second part with the If/Else – aynber Jul 15 '16 at 15:41