Php predefined vars, how to speak with php - PHP / MySQL Tutorials
 
  Buy Ebooks, Video Games, DVDs, Electronics and more !
Home | Help Forums | Web Design Tutorials | Free Swishmax Downloads Make Money Online Blog . Free Image and Photo Sharing and Hosting

 

 

Company

Home

Nework Details

Tutorials

SWiSH 2

SWiSHMax

Flash MX

PHP & MySQL

HTML / JScript

Adobe Photoshop

Corel Draw

Gimp

Miscellaneous

Downloads

Templates

Plugins

Fonts

Wallpapers

Free Images

Scripts and Codes

Products

E-Books

SWiSH BB

SWiSH Templates

SWiSH-DB Newsletter
Subscribe to our newsletter : GO
  Partners / Affiliates

   Free image hosting
   Web Design Pakistan
   Swish Templates
   SWiSH Climax
   Talkfreelance Community
   Template Desire
   Lite boards
   Script Sector
   GimpTalk
   13dots
   Idea Designs
   PHP Cafe
   Sposatoettore
   Free swishmax templates
   Swishzone

Manage your site with Flax Article Content Management System
Php predefined vars, how to speak with php
Category : PHP / MySQL | Level : Beginner | Language : English
Ask Question | Read Comments | Post tutorial | Previous | Next

Web www.swish-db.com

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
CODE
toto
titi

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
CODE
cortex
go

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=go

If 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'];
?>

CODE
cortex
toto
go


------------------------
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
CODE
cortex
go

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
CODE
cortex
toto
go


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


© 2002-2005 Flaxweb Network | Article Manager | Gimp Tutorials Library | RAD C++ Library | Free C/C++ Sourcecode |
Sites of interest : Web Design Blog | Swish Templates | Swishmax Ebook | Photoshop Templates | Gimp Tutorials | Text Forum | Make Money Online