TinyPortal

Development => Block Codes => Topic started by: iowamf on February 01, 2007, 09:15:30 PM

Title: simple form in a PHP article
Post by: iowamf on February 01, 2007, 09:15:30 PM
I plan on making a relatively complex form, but I want to start simple and get the bugs out first.

Below are two examples of a simple form in PHP - would be interesting to hear any comments/feedback.

Both forms have the same user IO, the second one is a little more complex and has more comments.


echo '<h4>Simple Form 4 with no ACTION </h4>';
if (!isset($_POST['submit'])) {
echo '
<form method="post">
enter something: <input type="text" name="blah" />
<input type="submit" name="submit" value="submit" />
<input type="reset" value="clear" />
</form>';
} else {
$some_string = $_POST["blah"];
echo "something entered was: $some_string <br />";
}


echo '<h4>Simple Form 3 with ACTION not using PHP_SELF</h4>';

// if this is done here:
// $some_string = $_POST["blah"];
// it will work ... but an error log entry will show up:
// Undefined index: blah


// setup the ACTION string

// get what page we are on (the info after index.php in the URL)
$env = getenv("QUERY_STRING");
if ($env) {
  // we are not on the homepage
  // set up to return user to the same page as the form
  $action_string =  'index.php?'.$env;
  // note - $action_string = "" returns user to the same page
  //          as the form too - why is the env needed?
  }
  else
  {
  // return user to the home page after the form is submitted
  $action_string = "index.php";
  // return user to some other page after the form is submitted
  // $action_string = "index.php?cat=23";
  }

if (!isset($_POST['submit']))
  {
  // if data has not been posted - echo the form
  echo '
<form method="post" action="' . $action_string . '">
enter something: <input type="text" name="blah" />
<input type="submit" name="submit" value="submit" />
<input type="reset" value="clear" />
</form>';
  }
  else
  {
  // data has been posted - process the form
  $some_string = $_POST["blah"];
  echo "something entered was: $some_string <br />";
  // double quotes vs single quotes usage
  echo 'env is '.$env.' action_string is '.$action_string.'<br />';
  }


I've seen some examples using the getenv("QUERY_STRING") to set the ACTION to return to the same page as the form - but if the action string is empty, it returns to the same page as the form - it seems like a waste?

Thanks for any feedback.