0

I am trying to learn Self Referencing Forms. I have read that a html form embedded in a php script is a self referencing form. I am still unable to pick the concept. Almost all the forms that I see in php codes are built with html. Is there something more specific about Self Referencing forms then just html forms being embedded in php script?

TimWolla
  • 31,849
  • 8
  • 63
  • 96
  • possible duplicate of [Blank HTML form action (posting back to self)](http://stackoverflow.com/questions/1131781/blank-html-form-action-posting-back-to-self) – Gordon Jan 22 '12 at 12:20

1 Answers1

2

You probably mean something like this:

<?php
if (count($_POST)) {
echo 'You have submitted the string: '.$_POST['string'];
}
?>
<form action="" method="post">
<input type="text" name="string">
<button type="submit">Submit!</button>
</form>

The empty action attribute causes the browser to submit it to the same URL as the one that is loaded. Via count($_POST) we check whether the form was submitted and act accordingly.

TimWolla
  • 31,849
  • 8
  • 63
  • 96
  • 1
    If you want to submit the form to the very same URL you may leave the action property empty. It's valid (x)html and you don't need to know the current URL. – Maerlyn Jan 22 '12 at 11:42
  • @TimWolla: Can you please explain how does the form loops back again once a user clicks submit? Thanks –  Jan 22 '12 at 11:52
  • The explanation was given by @Maerlyn – TimWolla Jan 22 '12 at 11:53
  • 1
    You should use `if ($_SERVER['REQUEST_METHOD'] === 'POST')` instead of `if (count($_POST))`. – FtDRbwLXw6 Jan 22 '12 at 14:00