5.Forms

1. Superglobal Arrays
Array
$_COOKIE
-Contains keys and values set as browser cookies
$_ENV
-Contains keys and values set by the script's shell context
$_FILES
-Contains information about uploaded files
$_GET
-Contains keys and values submitted to the script using the HTTP get method
$_POST
-Contains keys and values submitted to the script using the HTTP post method
$_REQUEST
-A combined array containing values from the $_GET, $_POST, and $_COOKIES superglobal arrays
$_SERVER
-Variables made available by the server
$GLOBALS
-Contains all global variables associated with the current script


2. An example for $_SERVER
<?php 
foreach ( $_SERVER as $key=>$value ) { 
 print "\$_SERVER[\"$key\"] == $value<br/>";
}
?> 


3. $_SERVER Variables
$_SERVER['PHP_SELF']
-contains the current script. Suitable for use in links and form element action arguments.
$_SERVER['HTTP_USER_AGENT']
-contains the name and version of the client.
$_SERVER['REMOTE_ADDR']
-contains the IP address of the client.
$_SERVER['REQUEST_METHOD']
-contains whether the request was GET or POST.
$_SERVER['QUERY_STRING']
-contains for GET requests, the encoded data sent appended to the URL.
$_SERVER['REQUEST_URI']
-contains the full address of the request, including query string.
$_SERVER['HTTP_REFERER']
-contains the address of the page from which the request was made.


4. Sending the data
<!DOCTYPE html PUBLIC     "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 
<title>A Simple HTML Form</title> 
</head> 
<body> 
 <form action=“process.php" method="get"> 
<input type="text" name="user"/>
<textarea name="address" rows="5" cols="40"> </textarea> 
<input type="submit" value="hit it!" /> 
</form> 
</body> 
</html> 


5. Capturing the data
<?php print "Welcome <b>".$_GET['user']."</b><br/>\n\n"; 
print "Your address is: <br/><b>".$_GET['address']."</b>"; 
?> 


6. Sending array of data
<form action="process.php" method="post">
<input type="text" name="user" />
   <input name="address" type="text" value="
 " size="40" />
   <select name="products[]" multiple="multiple">
     <option>Sonic Screwdriver</option>
     <option>Tricorder</option>
     <option>ORAC AI</option>
     <option>HAL 2000</option>
   </select>
<input type="submit" value="hit it!" />
 </form>


7. Capturing array of data
<?php
 print "Welcome <b>".$_POST['user']."</b><br/>\n";
 print "Your address is:<br/><b>".$_POST['address']."</b><br/>\n"; 
 if ( is_array( $_POST['products'] )  ) {
 print "<p>Your product choices are:</p>\n";
 print "<ul>\n";
     foreach ( $_POST['products'] as $value ) {
        print "<li>$value</li>\n";
     }
 print "</ul>\n";
}
 ?>


8. PHP that calls itself
<?php
 if ( ! empty( $_POST['guess'] ) ) {
     print "last guess: ".$_POST['guess'];
 }
 ?>
 <form method="post" action="<?php print $_SERVER['PHP_SELF']?>">
 <p>
 Type your guess here: <input type="text" name="guess" />
 </p>
 </form>


9. Using Hidden Fields
<form method="post" action="<?php print $_SERVER['PHP_SELF']?>">
<input type="hidden" name="num_tries" value="<?php print $num_tries?>" />
Type your guess here: <input type="text" name="guess" value= "<?php print $guess?>"/>
</form>


10. Redirecting Pages 
header("Location:congrats.html");


11. File Upload Form
form enctype="multipart/form-data"   action="<?print $_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="102400" />
<input type="file" name="fupload" />
<input type="submit" value="upload!" />
</form> 


12. $_FILES Elements
$ FILES['fupload']['name']
-contains name of uploaded file
$_FILES['fupload']['tmp_name']
-contains path to temporary file
$_FILES['fupload']['size']
-contains the size (in bytes) of uploaded file
$_FILES['fupload']['error']
-contains an error code corresponding to a PHP constant
$_FILES['fupload']['type']
-contains MIME type of uploaded file (where given by client)


13. File upload example
<?php if ( isset( $_FILES['fupload'] ) ) 
{     
print "name: ".     $_FILES['fupload']['name']       ."<br />";    
print "size: ".     $_FILES['fupload']['size'] ." bytes<br />";     
print "temp name: ".$_FILES['fupload']['tmp_name']   ."<br />";     
print "type: ".     $_FILES['fupload']['type']       ."<br />";     
print "error: ".    $_FILES['fupload']['error']      ."<br />";     
if ( $_FILES['fupload']['type'] == "image/gif" ) {        
$source = $_FILES['fupload']['tmp_name'];         
$target = "upload/".$_FILES['fupload']['name'];
move_uploaded_file( $source, $target );// or die ("Couldn't copy");        
$size = getImageSize( $target );        
$imgstr = "<p><img width=\"$size[0]\" height=\"$size[1]\" ";         
$imgstr .= "src=\"$target\" alt=\"uploaded image\" /></p>";
print $imgstr;     
} } ?> 

1 comment: