Using Variables Collected from Forms

Your web hosting server contains several global arrays which hold variables submitted by forms. These include:

The $_POST array is used when variables are submitted by the post method, etc. The array indices are the names given to the <input> or <select> tags of the form.

When you submit a form, these variables are immediately available for use by the program specified by the form. For example:

<form method="post" action="formres3.php">
 First Name:<input type="text" name="firstname"><br>
 Last Name: <input type="text" name="lastname"><br>
          <input type="Submit" value="Submit Data">
</form>
First Name:
Last Name:

The program "formres3.php" called to deal with the results is simply:

<?php
echo $_POST[firstname]." ".$_POST[lastname];
?>

A Tip for Using Form Variables

As you write programs using form input, it can be tricky to remember what the input variables were. It helps to list them at the beginning of each program. One way to do this is to rename them.

<?php
$firstName=$_POST[firstname];
$lastName=$_POST[lastname];
  echo $firstname." ".$lastname;
?>

Renaming the variables in this way is particularly helpful when the variable itself is an array. It lessens the possibility that php will misinterpret the multidimensional form of the array.

Uploading Arrays

At times you can upload arrays. I have had good success using the <select multiple name=varName[]> tag to do this.

I have not been successful in finding a general method of doing so. When I have to upload a JavaScript array, I upload it one variable at a time. An example is shown in Click Capture

Uploading Files with a Form

A file may be uploaded with html code such as this:

<form method="post" enctype="multipart/form-data" action="moveFile.php">
Upload the file.<br>
<input type="file"  name="upFile"><br>
<input type="submit" value="Finish">
</form>

When the upload is successful the $_FILES array will look something like this:

Array([upFile]=> Array( [name]=> dir.txt
                    [type]=> text/plain
                    [tmp_name]=>D:\Temp\phpD841.tmp
                    [error]=>0
                    [size]=>9309
                    )
     )
The file goes into a temporary directory. It is accessed by the action file. The action file may either move it to a permanent location, or read it.

Moving Uploaded Files

To move the file to a permanent location, the action file can use the php function move_uploaded_file(oldURL,newURL) . This function works only on files uploaded by the POST method. Furthermore, it only works if you have file privileges on your server. This snippet of code moves an uploaded file to the directory '../temp3/'

<?php
$tempFile=$_FILES['upFile']['tmp_name'];
$destFile=$_FILES['upFile']['name'];
$destFile='../temp3/'.$destFile;
echo "$tempFile \n";
echo "$destFile \n";
if (move_uploaded_file($tempFile, $destFile)) {
    echo "File moved.\n";
} else {
    echo "File not moved.\n";
}
echo 'Here is the $_FILES array:\n';
echo '<pre>';
print_r($_FILES);
echo '</pre>';
?> 

Upload the file.

Note: When checking your server for the uploaded file, be sure to refresh it first.

Reading Uploaded Files

Files can be read, and the data used, without keeping them on the server. With PHP 5, files can be opened and read with the file('filename') function. The result is an array, each item of which corresponds to one line of the file.

<?php 
$content=file("D:\Temp\phpD841.tmp");
 foreach($content as $var){ 
  echo($var);
  echo'<br>';
}
?>