PHP Form Demo

Example

Name the following file as index.htm file.


       <!DOCTYPE html5>/* ja v  a2  s  . c  om*/
       <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> //from   jav a  2 s . c  om
<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> 

Next chapter...

What you will learn in the next chapter:

  1. Why do we need to care about the empty fields
  2. Action for empty fields
Home » PHP Tutorial » PHP Form
PHP HTML Forms
PHP GET and POST
PHP Form Elements
PHP Form TextField
PHP Form Password Field
PHP Form Textarea
PHP Form CheckBox
PHP Form Select
PHP Form RadioButton
PHP Form Submit
PHP Form Reset
PHP Form Push Button
PHP Form Image
PHP Form Hidden Field
PHP Split Form Across Pages
PHP Form Validation
PHP Form File Upload
PHP Form Demo
PHP Form Empty Fields
PHP Form Multi-Value Fields
PHP Redirect after a Form Submission