PHP MySQL Tutorial
Learn PHP and MySQL

PHP Forms

75% of people found this useful
PHP Forms

Using forms in a web based application is very common. Most forms are used to gather information like in a signup form, survey / polling, guestbook, etc.

A form can have the method set as post or get. When using a form with method="post" you can use $_POST to access the form values. And when the form is using method="get" you can use $_GET to access the values. The $_REQUEST superglobal can be used to to access form values with method="post" and method="get" but it is recommended to use $_POST or $_GET instead so you will know from what method did the values come from.

Here is an example of HTML form :

 

 

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name : <input name="username" type="text"><br>
Password : <input name="password" type="password"><br>
<input name="send" type="submit" value="Send!">
</form>

The form above use $_SERVER['PHP_SELF'] as the action value. It is not required for the form to perform correctly but it's considered good programming practice to use it.

Below is the PHP code used to access form values :
 

<?php
if(isset($_POST['send']))
{
   echo "Accessing username using POST : " .         $_POST['username'] . "<br>";
   echo "Accessing username using REQUEST : " .         $_REQUEST['username'] . "<br>";

   $password = $_POST['password'];
   echo "Password is $password";
}
?>

The if statement is used to check if the send variable is set. If it is set then the form must have been submitted. The script then print the value of username using $_POST and $_REQUEST

 

Using Array As Form Values

Take a look at the code example below. The form have five input with the same name, language[]. Using the same input name is common for checkboxes or radio buttons.

 

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Select the programming languages you can use<br>
<input name="language[]" type="checkbox" value="C++">
C++<br>
<input name="language[]" type="checkbox" value="Java">
Java<br>
<input name="language[]" type="checkbox" value="PHP">
PHP<br>
<input name="language[]" type="checkbox" value="ASP">
ASP<br>
<input name="language[]" type="checkbox" value="Delphi">
Delphi<br>
<input name="send" type="submit" id="send" value="Send!">
</form>

The PHP code below print the value of language after the form is submitted. Go ahead and try the example. Try checking and unchecking the options to see the effect.

Example : form-array.php
Source code : form-array.phps

<?php
if(isset($_POST['language']))
{
   $language = $_POST['language'];
   $n        = count($language);
   $i        = 0;

   echo "The languages you selected are \r\n" .
        "<ol>";
   while ($i < $n)
   {
      echo "<li>{$language[$i]}</li> \r\n";
      $i++;
   }
   echo "</ol>";
}
?>

From the above code you will notice that $language is an array. This is because in the form code language is repeated several times. When you specify the same input name in a form, PHP will treat it as an array.

There is one important issue you should know when using forms, that is form validation. The code above does not check whether the input is correct such as checking if the user is entering any value into the textbox or is the user selecting any checkboxes. This is actually a bad practice because you should never put a web form without any validation. To learn about validating form input you can go to this page : Form Validation With PHP

Recent Comments

By: BrianNY Posted on 03-11-2009 8:57 AM

This relatively short article is a great tutorial on using forms in PHP.  It helped me enormously by showing me two real examples that worked in my home learning environment (WAMPSERVER).

You really explain each part of the programs well enough to follow and reason through without "beating it to death," or getting lost in the details so deep that one can't think straight.

Bottom line .. I now feel I know basic form definition and data retrieval.  I'll take it from here.  Thanks for getting me off the ground, making me feel comfortable with the coding.

By: Juan Carlos Posted on 03-23-2009 6:15 PM

Thanks BrianNY, it article is relly useful.  Can you write any similarity to it, but using a mysql database ??

Thanks!

By: peterv Posted on 07-02-2009 11:46 AM

Interesting article.  I'm a newbie to this stuff, so I'm not sure how these code snippets need to be put together to actually test them.  Any chance someone can add some notes on how to put these together to actually run them?

By: Magnus Posted on 08-13-2009 7:32 AM

Good article. Explains a lot.

If you have a small login form which calls a php script and that script opens a new full fledged login form, how can one assign variables from the script into the new full fledged login form. My idea is to generate a database call in the script to retrieve a user, and then add those fields to the login form.

Hope you can help

MAgnus

Powered by Community Server (Non-Commercial Edition), by Telligent Systems