PHP Tutorial - PHP HTML Forms






An HTML form is a collection of HTML elements embedded within a page. By adding different types of elements, you can create different form fields, such as text fields, pull-down menus, checkboxes, and so on.

All Web forms start with an opening <form> tag, and end with a closing </form> tag:

<form action="myscript.php" method="POST">  
     <!-- Contents of the form go here -->  
</form>  

Form attributes

There are two attributes within the opening <form> tag:

  • action - tells the browser where to send the form data. This should either be an absolute URL or a relative URL.
  • method - tells the browser how to send the form data. You can use two methods: GET is for sending small amounts of data and makes it easy for the user to resubmit the form, and POST can send much larger amounts of form data.
<form action="someform.php" method="post">
  Name: <input type="text" name="Name" value="Jim" /><br />
  Password: <input type="password" name="Password" /><br />
  Age: <input type="text" name="Age" /><br />
  <input type="submit" />
</form>




PHP GET and POST

To read the data from a form, we can use the following three superglobal variables.

Superglobal ArrayDescription
$_GETContains all the field names and values sent by a form using the get method
$_POSTContains all the field names and values sent by a form using the post method
$_REQUESTContains the values of both the $_GET and $_POST arrays combined, along with the values of the $_COOKIE superglobal array

Each of these three superglobal arrays contains the field names from the sent form as array keys, with the field values themselves as array values.

<input type="text "  name="emailAddress" value="" />  

You could then access the value that the user entered into that form field using either the $_GET or the $_REQUEST superglobal:

$email = $_GET["emailAddress"]; 
$email = $_REQUEST["emailAddress"];     




Different between GET and POST

GET sends its variables in the URL. It easy to see what was sent with GET. And user can change what was sent There is usually a low limit on the number of characters that can be sent in a URL. If we try to send long variables using GET, we may lose some of them.

POST sends its variables behind the scenes and has a much higher limit. It limit is usually several megabytes.

Browsers will not automatically resend post data if your user clicks the Back button, you may get a message like "The data on this page needs to be resent". This does not happen with GET, browsers will just resend data as needed through URL.

PHP has post_max_size entry in php.ini. It is usually set to 8M by default, which is 8 megabytes.

Example 2

Name the following file as index.htm file.

       <!DOCTYPE html5>
       <html> 
         <body> 
           <form action="index.php" method="post"> 
               <label for="firstName">First name</label> 
               <input type="text" name="firstName" id="firstName" value="" /> 

               <label for="lastName">Last name</label> 
               <input type="text" name="lastName" id="lastName" value="" /> 

               <label for="password1">Choose a password</label> 
               <input type="password" name="password1" id="password1" value="" /> 
               <label for="password2">Retype password</label> 
               <input type="password" name="password2" id="password2" value="" /> 

               <label for="genderMale">Are you male...</label> 
               <input type="radio" name="gender" id="genderMale" value="M" /> 
               <label for="genderFemale">...or female?</label> 
               <input type="radio" name="gender" id="genderFemale" value="F" /> 

               <label for="favoriteWidget">What's your favorite widget?</label> 
               <select name="favoriteWidget" id="favoriteWidget" size="1"> 
                 <option value="superWidget">The SuperWidget</option> 
                 <option value="megaWidget">The MegaWidget</option> 
                 <option value="wonderWidget">The WonderWidget</option> 
               </select> 

               <label for="newsletter">Do you want to receive our newsletter?</label> 
               <input type="checkbox" name="newsletter" id="newsletter" value="yes" /> 

               <label for="comments">Any comments?</label> 
               <textarea name="comments" id="comments" rows="4" cols="50"> </textarea> 
               <div style="clear: both;"> 
                 <input type="submit" name="submitButton" id="submitButton"  value="Send Details" /> 
                 <input type="reset" name="resetButton" id="resetButton" value="Reset Form" style="margin-right: 20px;" /> 
               </div> 
           </form> 
         </body> 
       </html> 

Next, save the following script as index.php in the same folder with index.html.

<!DOCTYPE html5> 
<html> 
  <body> 
    <p>Thank you for registering. Here is the information you submitted:</p> 
    <dl> 
      <dt>First name</dt><dd><?php echo $_POST["firstName"]?></dd> 
      <dt>Last name</dt><dd><?php echo $_POST["lastName"]?></dd> 
      <dt>Password</dt><dd><?php echo $_POST["password1"]?></dd> 
      <dt>Retyped password</dt><dd><?php echo $_POST["password2"]?></dd> 
      <dt>Gender</dt><dd><?php echo $_POST["gender"]?></dd> 
      <dt>Favorite widget</dt><dd><?php echo $_POST["favoriteWidget"]?></dd> 
      <dt>Do you want to receive our newsletter?</dt><dd><?php echo $_POST["newsletter"]?></dd> 
      <dt>Comments</dt><dd><?php echo $_POST["comments"]?></dd> 
    </dl> 
  </body> 
</html>