Home
XHTML
CSS
PHP
MySQL
SEO
JavaScript
Computer Basics
Number Systems
LINUX


PHP Forms

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...

<?php

echo "<form action='file.php' method='post'>
<div>
Name: <input type='text' name='name' />
City: <input type='text' name='city' />
<input type='submit' value='Enter' />
</div></form>";

?>

The output is...

Name: City:

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...

<?php

$name=$_POST['name'];
$city=$_POST['city'];

echo 'I am ' . $name . ' and ' . $city . ' is where I live.';

?>

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...

<?php

echo "<form action='file.php' method='get'>
<div>
Name: <input type='text' name='name' />
City: <input type='text' name='city' />
<input type='submit' value='Enter' />
</div></form>";

$name=$_GET['name'];
$city=$_GET['city'];

echo 'I am ' . $name . ' and ' . $city . ' is where I live.';

?>

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...

<input type="text" name="comment" value="<?php echo $comment ?>">

A variable can substitute values in a <select> list...

<?php

$ocean1='Pacific';
$ocean2='Indian';
$ocean3='Atlantic';

echo "<select name='ocean'>
<option>$ocean1</option>
<option>$ocean2</option>
<option>$ocean3</option>
</select>";

?>

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...

<?php

$zipcode=trim($zipcode);
$zipcodeLength=strlen($zipcode);

if($zipcodeLength != 5)
{echo 'Please enter a zipcode that is 5 characters.';}

?>

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...

<?php

function safe_output($string)
{$string=trim($string);
$string=strip_tags($string);
$string=htmlspecialchars($string);
return $string;}

?>

The  strip_tags()  function can have parameters that allow specified XHTML tags. This statement allows <strong>, but not <em>...

<?php

echo (strip_tags('<strong><em>text here</em></strong>', '<strong>'));

?>

The  strtoupper()  function makes a string uppercase. The  strtolower()  function makes a string lowercase...

<?php

echo (strtoupper('uppercase'));
echo "<br />";
echo (strtolower('lowercase'));

?>

The output is...

UPPERCASE
LOWERCASE



Email is sent with the  mail()  function. The  \n  is declared in the header for a line break...

<?php

mail('webmaster@website.com', 'PHP subject', 'PHP message',
     "From: user@website.com\n");

?>

This is another method of using the  mail()  function...

<?php

$to='webmaster@website.com';
$subject='PHP subject';
$body='PHP message';
$headers="From: user@website.com\n";

mail($to, $subject, $body, $headers);

echo "Email sent to $to";

?>

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...

<form action='email.php' method='post'>
<div>
Email: <input type='text' name='email' /><br /><br />
Message: <br /><textarea rows='6' cols='18' name='message'>
</textarea><br /><br />
<input type='submit' />
</div></form>

The output is...

Email:

Message:


PHP processes the data submitted in the XHTML form...

<?php

$email=$_POST['email'];
$message=$_POST['message'];

mail('webmaster@website.com', 'PHP Subject',
$message, "From: $email" );

echo '<br /><br />';
echo "Email sent from $email";

?>

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...

<form enctype='multipart/form-data' action='upload.php' method='post'>
<div>
<input type='hidden' name='MAX_FILE_SIZE' value='90000' />
Upload File:<br />
<input type='file' name='userfile' /><br /><br />
<input type='submit' value='Upload' />
</div></form>

The output is...

Upload File:


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...

<?php

$uploadDirectory='./';
$uploadFile=$uploadDirectory . basename($_FILES['userfile']['name']);

echo '<div>';

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile))
{echo "<br /><br />The file has been uploaded!<br /><br />";}
echo '</div><br />';

$name=$_FILES['userfile']['name'];
if($_POST){echo "<img src='./" . $name . " ' alt='alt' />";}

?>

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...

<?php

$uploadDirectory='./';
$uploadFile=$uploadDirectory . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile))
{echo "<br /><br />The file has been uploaded!<br /><br />";}
else
{switch ($_FILES['userfile'] ['error'])
{case 1:
echo '<p>The file is bigger than PHP allows!</p>';
break;
case 2:
echo '<p>The file is bigger than the form allows!</p>';
break;
case 3:
echo '<p>Only part of the file was uploaded!</p>';
break;
case 4:
echo '<p>No file was uploaded!</p>';
break;}}
echo '<br />';

$name=$_FILES['userfile']['name'];
$size=$_FILES['userfile']['size'];
$type=$_FILES['userfile']['type'];
if($_POST){echo "<img src='./" . $name . " ' alt='alt' />";}

echo "<br /><br />";
if($_POST){echo "The file name is:&nbsp; $name";}
echo "<br /><br />";
if($_POST){echo "The file size is:&nbsp; $size bytes";}
echo "<br /><br />";
if($_POST){echo "The file type is:&nbsp; $type bytes";}

?>

If the file upload is successful, the output to the browser is...

File Uploaded.




SEO Vancouver, Washington
SEO Portland, Oregon
Website Design Vancouver, Washington