Ciemny, Jessica wrote:
Hey every, I'm new to the group and to PHP (3 days now) and I would like to
ask a question. I purchased a book on PHP a day or so ago and as I'm going
through the book it comes to a point where it talks about passing html form
values to PHP. So I type up the examples from the book, both the html file
and the PHP file and I run it and I don't get anything when the PHP file is
accessed. Hmmmm puzzling! I don't get any errors at all, but the values just
aren't coming across.

The book says that I should be able to take the "name=xyz" value from the
HTML form and just use it as a regular variable ($xyz) in the PHP file. But
this doesn't work.


After much digging on web pages and through email lists I found reference to
something called the $_POST variable. This seems to be the only way that I
can make the PHP file use the "name=xyz" values from the HTML file. So in
the PHP file I would have to use the variable "$_POST[xyz]" to get the form
value returned.


Anyone have any ideas as to what's up. I hope its just something silly that
I'm missing being a newby to PHP and all.

The problem here is that the book is written for a PHP setup where register_globals is assumed to be turned on. This can be a security risk, so as of PHP 4.2.x, register_globals has been turned off by default (before it was on by default).


Register globals is a directive that tells PHP to automatically create variables from requested information (be it POST or GET). More information about why register_globals is a bad idea can be found at the following PHP manual page [ http://www.php.net/register_globals ]

I will include small versions of the file here for reference:

[ snipped ]


So I guess my question ultimately is, what are the different ways in which
form data can be passed to a PHP script?

Data is transferred using forms in two methods. GET or POST. The difference between the two (the main difference) is that the data from a GET request is appended to the URL. Such a request would be :


http://www.google.com/search?q=php+tutorials

and data from a POST request is actually sent as part of the HTTP request (in the header), so its not viewable in the browser URL.

PHP provides (since PHP version 4.2) two arrays, $_POST and $_GET for each of the above methods. There is also a third array, $_REQUEST which holds all information sent in the request.

These arrays are called "superglobals" because they are available throughout your pages without any effort from your part. Each array consists of a key and a value. The key is whatever is the name attribute of your form value.

An example :

<form method="POST" action="process.php">
<input type="text" name="firstname" size="10" />
<input type="submit" name="submit" value="submit" />
</form>

process.php :

<?php

  //Turn up error reporting
  //to the max level
  error_reporting(E_ALL);

  //Print out the contents
  //of the $_POST array
  //so we can see what it
  //contains.
  echo "<pre>"; print_r($_POST); echo "</pre>";
?>

When you run the above script with the associated form, you will see the format of the $_POST array. It will contain an element with the key "firstname" and the value will be whatever the user entered in that text box. It will also contain an element with the key "submit" whose value is "submit" (that's our submit button).

Hopefully this helps, and welcome to PHP :)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to