PHP |
Values |
Operators |
Functions |
Arrays |
Forms |
Sessions-Cookies |
Classes
Forms can be used to pass data through email, access data in a database, and upload files. Forms are built with XHTML. The data is processed with PHP. Any form field can be referenced with PHP...
The output is...
Input from the user goes to file.php to be processed, as defined by the action attribute. Input passed to file.php is stored in a global array called $_POST, as defined by the method attribute. Action and method are standard <form> attributes. Type and name are standard <input /> attributes. Type defines the <input /> field. Name labels the <input /> field, which is referenced by the index of the $_POST array. The input values submitted in the form are retrieved and stored in PHP variables...
If Anyname and Anycity are the submitted values for name and city, the output is...
I am Anyname and Anycity is where I live.
$_GET is a global array that is an alternative to $_POST, which appends data to the URL in a browser. $_GET is less secure and holds less data than $_POST. The $_GET method is commonly used for linking to other pages or files...
If Anyname and Anycity are the submitted values for name and city, the appended URL path is...
.../file.php?name=Anyname&city=Anycity
A PHP variable can substitute any value in an XHTML form...
A variable can substitute values in a <select> list...
The output is...
A variable can also control values in radio button, checkbox, and other <input /> types.
PHP has functions for validating. The trim() function removes character spaces from the beginning and end of a string, such as from a person's name submitted in a form. The strlen() function determines the length of the string...
If the data submitted is not equal to five characters, the output is...
Please enter a zipcode that is 5 characters.
The strip_tags() function removes XHTML and PHP tags from a string. The htmlspecialchars() function makes special XHTML characters safe for a browser. A user function can store multiple functions...
The strip_tags() function can have parameters that allow specified XHTML tags. This statement allows <strong>, but not <em>...
The strtoupper() function makes a string uppercase. The strtolower() function makes a string lowercase...
The output is...
UPPERCASE
LOWERCASE
Email is sent with the mail() function. The \n is declared in the header for a line break...
This is another method of using the mail() function...
The email is sent to the recipient. The output to the browser is...
Email sent to webmaster@website.com
Email can be submitted in an XHTML form...
The output is...
PHP processes the data submitted in the XHTML form...
If an email is sent by user@website.com, the output to the browser is...
Email sent from user@website.com
An image or file can be uploaded in an XHTML form. The enctype attribute defines binary data and is required in the form tag. The hidden <input /> field determines the file size. The name attribute defines the maximum file size allowed with the value as MAX_FILE_SIZE. The value attribute defines the size allowed in bytes. 90000 is the same as 90kb. The file <input /> field creates the browse button and is declared after the hidden <input /> field. The name attribute passes the uploaded file data to PHP...
The output is...
PHP process the data submitted in upload.php. The upload path directory is stored in the variable $uploadDirectory. The move_uploaded_file() function moves the file to the specified directory. The file data is stored in a global array called $_FILES. The ['userfile'] index references the name of the file defined by the name attribute in the file <input /> type of the XHTML form. The ['tmp_name'] index refers to the temporary file that is created until the file data is passed to $_FILES. The basename() function returns the file name from a path. $uploadDirectory and the basename() function can be stored in a separate variable. The variable in the example is $uploadFile...
These are the different elements of metadata available for $_FILES...
$_FILES['userfile']['name'] - name of uploaded file
$_FILES['userfile']['size'] - size in bytes of uploaded file
$_FILES['userfile']['type'] - type of uploaded file
$_FILES['userfile']['tmp_name'] - name of temporary copy of file
$_FILES['userfile']['error'] - error syntax resulting from file upload
This example uses the switch() function with case statements to output error messages to the browser. The uploaded image also outputs to the browser. The file metadata is optional...
If the file upload is successful, the output to the browser is...
File Uploaded.