Hi all, Now if you read tutorial so far, you should be able to do little things. The counter part is that it doesn't talk with outside world. Well, this is a practical tutorial so there will be a lot of example (i hope, well i'm motivated cause i just start writing).
In all this tutorial, i will use a php file named index.php. And i will deal with these so called predefined vars (the more interesting of them). Here a little description: they are both arrray named $HTTP_GET_VARS and $HTTP_POST_VARS. The first one results of the parameters you give in an url and the second one results of the data you send with a form with methode set to POST (<form action="index.php" method="POST" >.
Let start with url, cause it's the more easier.
| CODE | | <a href="index.php?myVar=toto&myVar2=titi">test</a> |
To pass parameters to php, we need to add ? symbol to the php filename and separate each pair 'name=value' by &. (sure it remember you something). Now for the php file
| CODE | <?php echo $HTTP_GET_VARS['myVar']; echo $HTTP_GET_VARS['myVar2']; ?> |
this will print
For each pairs, php will do the affectation shown by the pair. --------------- Forms let have this form
| CODE | <form action="index.php" method="POST"> <input type="text" name="myVar"/> <input type="submit" name="submit" value="go"/> </form> |
Now for the php file
| CODE | <?php echo $HTTP_POST_VARS['myVar']; echo $HTTP_POST_VARS['submit']; ?> |
I assume i have fill the form with cortex and press go button, the result appears like
For each element in the submitted form, php will do an affectation with the name of this element affected by the value of this element. Like
| CODE | $myVars=value of element text name myVar $submit='go'; |
you see it if you change the form method to GET, in the adress bar it look like this http://localhost/index.php?myVar=cortex&submit=goIf you have change the method of the form from POST to GET, you will need to use $HTTP_GET_VARS. It goes the same way, with swish | CODE | loadVariables('index.php', 'GET'); use $HTTP_GET_VARS; loadVariables('index.php', 'POST'); use $HTTP_POST_VARS; |
Oh, and you could use both at the same time.
| CODE | <form action="index.php?myVar=toto" method="POST"> <input type="text" name="myVar"/> <input type="submit" name="submit" value="go"/> </form> |
| CODE | <?php echo $HTTP_POST_VARS['myVar']; echo $HTTP_GET_VARS['myVar']; echo $HTTP_POST_VARS['submit']; ?> |
------------------------ FAQ ------------------------ Why i never see this array in some php script like guesbook and mailform ? In some php configuration, the variable could be accessed directly by their named. An order define which variables will remain if their is a collision in name. Default is GET then POST: the POST variables will overwrite GET variables with the same name. In the example above, if we do
| CODE | <?php echo $myVar; echo $submit; ?> |
will print
Think that it's not a good idea to continue this way cause last version of php disable this feature for security reason. (since v4.2.0)
------------------------ Why i couldn't access HTTP_x_VARS in my function ? As any other variables, they're not global. so you must add global $HTTP_X_VARS in your function body.
------------------------- Someone tell me that there's some array that are always global, what's this ? In the last version of php (since v4.1.0), $HTTP_POST_VARS and $HTTP_GET_VARS are being replaced by $_POST and $_GET arrays. This is a 'superglobal' variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $_GET, $_POST; to access it within functions or methods, as you do with $HTTP_GET_VARS, $HTTP_POST_VARS.
| CODE | <?php function printThem(){ echo $_POST['myVar']; echo $_GET['myVar']; echo $_POST['submit']; } printThem(); ?> |
will print
If your server support it (php v4.1.0 or latter), use it cause it will be the standard. I start with $HTTP_x_VARS and now have some problem to forget them. If use one solution, keep on it and don't mix them.
----------------------- Ok, for now you now how to handle simple form. Next tut will explain how to handle complex form with checkbox, radiobutton, single listvalue and multiple ones. Stay on tunes. Any questions or suggestion, just ask.
regards, Sébastien
|