Page 154 - Open Soource Technologies 304.indd
P. 154

Unit 9: Working with Forms



            do great things with a database. But that’s all meaningless if users can’t reach into a language’s   Notes
            environment to offer it information. In this, you look at strategies for acquiring and working with
            user input. On the World Wide Web, HTML forms are the principal means by which substantial
            amounts of information pass from the user to the server. In this you will learn:

               •  How to access information from form fields?
               •  How to work with form elements that allow multiple selections?
               •  How to create a single document that contains both an HTML form and the PHP code
                 that handles its submission?
               •  How to save state with hidden fields?

               •  How to redirect the user to a new page?

               •  How to build HTML forms and PHP code that send mail?
               •  How to build HTML forms that upload files and how to write the PHP code to handle
                 them?

            9.1 Creating a Simple Input Form

            For now, let’s keep our HTML separate from our PHP code. Listing 1 builds a simple HTML form.

            Listing 1 A Simple HTML Form
               1.  <html>

               2.  <head>
               3.  <title>Listing 1 A simple HTML form</title>
               4.  </head>

               5.  <body>
               6.  <form action=”listing2.php” method=”POST”>

               7.  Name: <br>
               8.  <input type=”text” name=”user”>
               9.  <br>

              10.  Address: <br>
              11.  <textarea name=”address” rows=”5” cols=”40”></textarea>

              12.  <br>
              13.  <input type=”submit” value=”hit it!”>
              14.  </form>

              15.  </body>
              16.  </html>

            Put these lines into a text file called listing 1, and place that file in your Web server document
            root. This listing defines a form that contains a text field with the name “user” on line 8, a text
            area with the name “address” on line 11, and a submit button on line 13. The FORM element’s



                                             LOVELY PROFESSIONAL UNIVERSITY                                   149
   149   150   151   152   153   154   155   156   157   158   159