Page 497 - Beginning PHP 5.3
P. 497

Chapter 15: Making Your Job Easier with PEAR
                                < head >
                                  < title > Simple HTML_QuickForm Example < /title >
                                < /head >
                                < body >
                                  < h1 > Simple HTML_QuickForm Example < /h1 >
                               < ?php
                             require_once( “HTML/QuickForm.php” );
                             $form = new HTML_QuickForm( “”, “post”, “”, “”, null, true );
                             $form- > addElement( “text”, “username”, “Username” );
                             $password = $form- > addElement( “password”, “password”, “Password” );
                             $password- > setValue( “” );
                             $buttons = array();
                             $buttons[] = HTML_QuickForm::createElement( “submit”, “submitButton”, “Send
                             Details” );
                             $buttons[] = HTML_QuickForm::createElement( “reset”, “resetButton”, “Reset
                             Form” );
                             $form- > addGroup( $buttons, null, null, “ & nbsp;” );

                             if ( $form- > isSubmitted() ) {
                               echo “ < p > Thanks for your details! < /p > ”;
                             } else {
                               echo $form- > toHtml();
                             }
                             ? >
                                < /body >
                               < /html >

                           After displaying the page header, the script includes the  HTML/QuickForm.php  class file, then creates a
                          new   HTML_QuickForm  object with a blank  name  attribute, a  method= “ post ”   attribute, empty  action
                         and   target  attributes, no additional attributes, and the  $trackSubmit  property set to  true  so that the
                          script can detect when the form has been submitted:

                             require_once( “HTML/QuickForm.php” );

                             $form = new HTML_QuickForm( “”, “post”, “”, “”, null, true );
                                                                                                 :
                           Next, the script adds a  username  text input field to the form, with a label of   “ Username “
                             $form- > addElement( “text”, “username”, “Username” );


                           A password input field called  password  is also added, with a label of   “ Password .  By storing the
                                                                                          ”
                         returned element object in a variable,   $password , the script can then set the field ’ s value to an empty
                         string:
                             $password = $form- > addElement( “password”, “password”, “Password” );
                             $password- > setValue( “” );
                           It ’ s a good idea to do this to prevent the password being sent back to the browser  —  and therefore being
                         viewable in the page source  —  if the form is redisplayed.








                                                                                                         459





                                                                                                      9/21/09   9:14:53 AM
          c15.indd   459                                                                              9/21/09   9:14:53 AM
          c15.indd   459
   492   493   494   495   496   497   498   499   500   501   502