Learn web design and programming with our free video and text tutorials.

Web Designer? Design and market your own professional website with easy-to-use tools.

PHP Forms Tutorial

PHP can be used to access data that is submitted by an (X)HTML form to a script on your web server.

When a form is submitted, each named form element and its value are converted to key-value pairs and sent along as part of a POST or GET HTTP request.

When a GET or POST request is sent to the script, all variables are accessible using either of the built-in $_GET array or $_POST array.

The $_GET array is a globally accessible variable. The $_GET array is an associative variable, meaning that each form element submitted by the web form can be accessed by name.

Create a form in your document containing the form below:

<form action="phpandformstutorialexample.php" method="post">

Name: <input type="text" name="name" />
<br />
Email: <input type="text" name="age" />
<br />
Subject: <input type="text" name="subject" />
<br />
<textarea name="message" cols="55" rows="10"> </textarea>
<br />
<input type="submit" />


The code above creates the form below:


Create a seperate php document with the code below. The document should have the same name as the value of the action attribute in the form above, which is "phpandformstutorialexample.php" in this example.

My name is: <?php echo $_POST["name"]; ?>
<br />
My email is: <?php echo $_POST["age"]; ?>
<br />
The subject is: <?php echo $_POST["subject"]; ?>
<br />
The message is: <?php echo $_POST["message"]; ?>

PHP Form Validation

When you make a form you should always include some type of form validation.

Form can be validated in 2 places: client side and server side.

Client side form validation is usually done with javascript. Client side validation is relatively faster than server side validation. Server side validation is usually used as a backup to client side validation in case the web browser has javascript turned off. There is also a possibility that the web browser will not execute the javascript as intended.