TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

May 18, 2024, 01:22:36 PM

Login with username, password and session length
Members
  • Total Members: 3,886
  • Latest: Grendor
Stats
  • Total Posts: 195,189
  • Total Topics: 21,220
  • Online today: 112
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 47
  • Total: 47

simple form in a PHP article

Started by iowamf, February 01, 2007, 09:15:30 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

iowamf

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.