Azroc Sitemap

 

 

 

 

 

 

Variables from outside PHP

HTML Forms (GET and POST) - Please leave a comment after reading.

When a form is submitted to a PHP script, in this case foo.php, the information from that form is automatically made available to the script. The information in the form can be accessed in the script foo.php for example:

A simple HTML form - code 1

<form action="foo.php" method="post">
    Name:  <input type="text" name="username" size="25" /><br />
    Email: <input type="text" name="email" size="30" /><br />
    <input type="submit" name="Submit button pressed" value="Submit" />
</form>

Depending on your particular setup and personal preferences, the information in the form can be accessed in the script foo.php from your HTML forms. Some examples are:

Accessing data from a simple POST HTML form - this could be the the file file.php - code 2

<?php  
	echo '</pre>'
	print_r($_POST);
	//this outputs what is contained in
	//$_POST the <pre> tags maintaining format
	echo '</pre>';
	
	if(isset($_POST['submit'])) //if the submit button was pressed 
  	{
       	echo 'For username you entered '.$_POST['username'];
       	echo '<br>';
  		echo 'For email you entered '.$_POST['email'];
  
  	}
	
	echo '<a href="php_forms.php">Go Back</a>';
?>
 

 

Try the above example:

Enter the details in the form and click the Submit button. This page will post to( be redirected ) to the file file.php which contains the php code exactly as typed above in code 2.

 

Your name
Your E-mail address

 

Using a GET form is similar except you'll use the appropriate GET predefined variable instead. GET also applies to the QUERY_STRING (the information after the '?' in a URL). So, for example, http://www.example.com/test.php?id=3 contains GET data which is accessible with $_GET['id'].

Note: $_POST and $_GET, became available in PHP 4.1.0

PHP also understands arrays in the context of form variables . You may, for example, group related variables together, or use this feature to retrieve values from a multiple select input. For example, let's post a form to itself and upon submission display the data:

More complex form variables

<?php
if (isset($_POST['action']) && $_POST['action'] == 'submitted') {
   echo
'<pre>';
  
print_r($_POST);
   echo
'<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';

   echo
'</pre>';
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   Name:  <input type="text" name="personal[name]" /><br />
   Email: <input type="text" name="personal[email]" /><br />
   Beer: <br />
   <select multiple name="beer[]">
       <option value="warthog">Warthog</option>
       <option value="guinness">Guinness</option>
       <option value="stuttgarter">Stuttgarter Schwabenbräu</option>
   </select><br />
   <input type="hidden" name="action" value="submitted" />
   <input type="submit" name="submit" value="submit me!" />
</form>
<?php
}
?>

 

To see the above code in action just use the form below. The $_SERVER['PHP_SELF'] variable causes this page to post to itself with any PHP code on it to be executed such as that which is above the form code in the above example. To see the result of this code enter some details and hit the submit me button. You can multiple select in the Beer textbox by holding down the Ctrl key.

 

Name: 
Email:
Beer:

 

In PHP 3, the array form variable usage is limited to single-dimensional arrays. As of PHP 4, no such restriction applies.

 

IMAGE SUBMIT variable names

 

When submitting a form, it is possible to use an image instead of the standard submit button with a tag like:

<input type="image" src="image.gif" name="sub" />

When the user clicks somewhere on the image, the accompanying form will be transmitted to the server with two additional variables, sub_x and sub_y. These contain the coordinates of the user click within the image. The experienced may note that the actual variable names sent by the browser contains a period rather than an underscore, but PHP converts the period to an underscore automatically.

HTTP Cookies

PHP transparently supports HTTP cookies. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. You can set cookies using the setcookie() function.

If you wish to assign multiple values to a single cookie variable, you may assign it as an array. For example:

<?php
  setcookie
("MyCookie[foo]", 'Testing 1', time()+3600);
 
setcookie("MyCookie[bar]", 'Testing 2', time()+3600);
?>

That will create two separate cookies although MyCookie will now be a single array in your script.

Note that a cookie will replace a previous cookie by the same name in your browser unless the path or domain is different. So, for a shopping cart application you may want to keep a counter and pass this along. i.e.

A setcookie() example

<?php
if (isset($_COOKIE['count'])) {
  
$count = $_COOKIE['count'] + 1;
} else {
  
$count = 1;
}
setcookie('count', $count, time()+3600);
setcookie("Cart[$count]", $item, time()+3600);
?>

Dots in incoming variable names

Typically, PHP does not alter the names of variables when they are passed into a script. However, it should be noted that the dot (period, full stop) is not a valid character in a PHP variable name. For the reason, look at it:

<?php
$varname
.ext/* invalid variable name */
?>
Now, what the parser sees is a variable named $varname, followed by the string concatenation operator, followed by the barestring (i.e. unquoted string which doesn't match any known key or reserved words) 'ext'. Obviously, this doesn't have the intended result.

For this reason, it is important to note that PHP will automatically replace any dots in incoming variable names with underscores.

eXTReMe Tracker

Links:

Pixel2life.com
PHP.net
Learn C#