| 1 | jonen | 1.1 | <?php | 
| 2 |  |  |  | 
| 3 |  |  | /** | 
| 4 |  |  | * This example illustrates the basics of the | 
| 5 |  |  | * FormProcessing engine for phphtmllib. | 
| 6 |  |  | * | 
| 7 |  |  | * | 
| 8 | jonen | 1.2 | * $Id: form1.php,v 1.5 2003/07/28 17:05:25 hemna Exp $ | 
| 9 | jonen | 1.1 | * | 
| 10 |  |  | * @author Walter A. Boring IV <waboring@buildabetterweb.com> | 
| 11 |  |  | * @package phpHtmlLib | 
| 12 |  |  | * @subpackage form-examples | 
| 13 |  |  | * @version 2.2.0 | 
| 14 |  |  | * | 
| 15 |  |  | */ | 
| 16 |  |  |  | 
| 17 |  |  | /** | 
| 18 |  |  | * Include the phphtmllib libraries | 
| 19 |  |  | * | 
| 20 |  |  | */ | 
| 21 |  |  | include("includes.inc"); | 
| 22 |  |  |  | 
| 23 |  |  | /** | 
| 24 |  |  | * Include the Form Processing objects | 
| 25 |  |  | * | 
| 26 |  |  | */ | 
| 27 |  |  | include_once($phphtmllib."/form/includes.inc"); | 
| 28 |  |  |  | 
| 29 |  |  |  | 
| 30 |  |  | //use the class we defined from | 
| 31 |  |  | //Example 3. | 
| 32 |  |  | include_once("MyLayoutPage.inc"); | 
| 33 |  |  |  | 
| 34 |  |  | /** | 
| 35 |  |  | * A simple Page Layout object child. | 
| 36 |  |  | * this came from Example 3. | 
| 37 |  |  | * | 
| 38 |  |  | * @author Walter A. Boring IV <waboring@buildabetterweb.com> | 
| 39 |  |  | * @package phpHtmlLib | 
| 40 |  |  | * @subpackage form-examples | 
| 41 |  |  | */ | 
| 42 |  |  | class Form1Page extends MyLayoutPage { | 
| 43 |  |  |  | 
| 44 |  |  | function content_block() { | 
| 45 |  |  | //build the FormProcessor, and add the | 
| 46 |  |  | //Form content object that the FormProcessor | 
| 47 |  |  | //will use.  Make the width of the form 600 | 
| 48 |  |  | return new FormProcessor( new AccountForm(600)); | 
| 49 |  |  | } | 
| 50 |  |  | } | 
| 51 |  |  |  | 
| 52 |  |  |  | 
| 53 |  |  | /** | 
| 54 |  |  | * This is the Class that handles the building | 
| 55 |  |  | * of the Form itself.  It creates the Form Elements | 
| 56 |  |  | * inside the form_init_elements() method. | 
| 57 |  |  | * | 
| 58 |  |  | * @author Walter A. Boring IV <waboring@buildabetterweb.com> | 
| 59 |  |  | * @package phpHtmlLib | 
| 60 |  |  | * @subpackage form-examples | 
| 61 |  |  | */ | 
| 62 |  |  | class AccountForm extends FormContent { | 
| 63 |  |  |  | 
| 64 |  |  | /** | 
| 65 |  |  | * This method gets called EVERY time the object is | 
| 66 |  |  | * created.  It is used to build all of the | 
| 67 |  |  | * FormElement objects used in this Form. | 
| 68 |  |  | * | 
| 69 |  |  | */ | 
| 70 |  |  | function form_init_elements() { | 
| 71 |  |  | //we want an confirmation page for this form. | 
| 72 |  |  | $this->set_confirm(); | 
| 73 |  |  |  | 
| 74 |  |  | //now start to add the Form Elements that will | 
| 75 |  |  | //be used in this form. | 
| 76 |  |  |  | 
| 77 |  |  | //Build the First Name Form Element.  It is just | 
| 78 |  |  | //a simple Text field.  It is not required for | 
| 79 |  |  | //the user to enter data for this field. | 
| 80 |  |  | $this->add_element( new FEText("First Name", FALSE, "200px") ); | 
| 81 |  |  |  | 
| 82 |  |  | //Build the Email address Field.  It is a required field | 
| 83 |  |  | //It will automatically get validated as an Email address | 
| 84 |  |  | //it can accept either foo@bar.net or Joe Blow <foo@bar.net> | 
| 85 |  |  | $this->add_element( new FEEmail("Email Address", TRUE, "200px") ); | 
| 86 |  |  |  | 
| 87 |  |  |  | 
| 88 |  |  | //now we need to build the password and confirm password | 
| 89 |  |  | //fields. | 
| 90 |  |  | $password = new FEPassword("Password", TRUE, "200px"); | 
| 91 |  |  | $this->add_element( $password ); | 
| 92 |  |  |  | 
| 93 |  |  | $confirm = new FEConfirmPassword("Confirm Password", TRUE, "200px"); | 
| 94 |  |  | //add the password FormElement to the ConfirmPassword | 
| 95 |  |  | //FormElement so we can make sure they match. | 
| 96 |  |  | $confirm->password( $password ); | 
| 97 |  |  |  | 
| 98 |  |  | //add it to this form. | 
| 99 |  |  | $this->add_element( $confirm ); | 
| 100 |  |  |  | 
| 101 |  |  | //lets add a hidden form field | 
| 102 |  |  | $this->add_hidden_element("id"); | 
| 103 |  |  | } | 
| 104 |  |  |  | 
| 105 |  |  | /** | 
| 106 |  |  | * This method is called only the first time the form | 
| 107 |  |  | * page is hit.  This enables u to query a DB and | 
| 108 |  |  | * pre populate the FormElement objects with data. | 
| 109 |  |  | * | 
| 110 |  |  | */ | 
| 111 |  |  | function form_init_data() { | 
| 112 |  |  | //Pull some valies from the DB | 
| 113 |  |  | //and set the initial values of some of the | 
| 114 |  |  | //form fields. | 
| 115 |  |  | // | 
| 116 |  |  | //In this example we just hard code some | 
| 117 |  |  | //initial values | 
| 118 |  |  |  | 
| 119 |  |  | $this->set_element_value("Email Address", "foo@bar.net"); | 
| 120 |  |  | $this->set_hidden_element_value("id", "hemna"); | 
| 121 |  |  | } | 
| 122 |  |  |  | 
| 123 |  |  |  | 
| 124 |  |  | /** | 
| 125 |  |  | * This is the method that builds the layout of where the | 
| 126 |  |  | * FormElements will live.  You can lay it out any way | 
| 127 |  |  | * you like. | 
| 128 |  |  | * | 
| 129 |  |  | */ | 
| 130 |  |  | function form() { | 
| 131 |  |  | $table = &html_table($this->_width,0,4); | 
| 132 |  |  | $table->set_style("border: 1px solid"); | 
| 133 |  |  |  | 
| 134 |  |  | $table->add_row($this->element_label("First Name"), | 
| 135 |  |  | $this->element_form("First Name")); | 
| 136 |  |  |  | 
| 137 |  |  | $table->add_row($this->element_label("Email Address"), | 
| 138 |  |  | $this->element_form("Email Address")); | 
| 139 |  |  |  | 
| 140 |  |  | $table->add_row($this->element_label("Password"), | 
| 141 |  |  | $this->element_form("Password")); | 
| 142 |  |  |  | 
| 143 |  |  | $table->add_row($this->element_label("Confirm Password"), | 
| 144 |  |  | $this->element_form("Confirm Password")); | 
| 145 |  |  |  | 
| 146 |  |  | $table->add_row(_HTML_SPACE, $this->add_action("Submit")); | 
| 147 |  |  |  | 
| 148 |  |  | return $table; | 
| 149 |  |  | } | 
| 150 |  |  |  | 
| 151 |  |  | /** | 
| 152 |  |  | * This method gets called after the FormElement data has | 
| 153 |  |  | * passed the validation.  This enables you to validate the | 
| 154 |  |  | * data against some backend mechanism, say a DB. | 
| 155 |  |  | * | 
| 156 |  |  | */ | 
| 157 |  |  | function form_backend_validation() { | 
| 158 |  |  | //$this->add_error("Ass", "some bogus error happened"); | 
| 159 |  |  | //return FALSE; | 
| 160 |  |  | return TRUE; | 
| 161 |  |  | } | 
| 162 |  |  |  | 
| 163 |  |  | /** | 
| 164 |  |  | * This method is called ONLY after ALL validation has | 
| 165 |  |  | * passed.  This is the method that allows you to | 
| 166 |  |  | * do something with the data, say insert/update records | 
| 167 |  |  | * in the DB. | 
| 168 |  |  | */ | 
| 169 |  |  | function form_action() { | 
| 170 |  |  | $this->set_action_message("WOO!"); | 
| 171 |  |  | //$this->add_error("Ass", "some bogus error happened"); | 
| 172 |  |  | //$this->add_error("First Name", "Duplicate!!  You suck!"); | 
| 173 |  |  |  | 
| 174 | jonen | 1.2 | //var_dump( $this->get_element_value("First Name") ); | 
| 175 | jonen | 1.1 | return TRUE; | 
| 176 |  |  | } | 
| 177 |  |  | } | 
| 178 |  |  |  | 
| 179 |  |  |  | 
| 180 |  |  | $page = new Form1Page("Form Example 1"); | 
| 181 |  |  | print $page->render(); | 
| 182 |  |  | ?> |