onGet & onSubmit
XFL
As outlined in the actions, triggers and events section form processing can be manipulated by adding onGet and onSubmit methods in the controler class.
onGet
Using the PHP file created in the form creation section, we will use onGet to populate a form from an array.
Two changes have been made to the PHP file for it to now populate a form with values.
The first change is an onGet method was added to the DemoForm class. Typically, the onGet methods would use a database query to populate the form. However, for this example an array has been used. The array returned by the onGet method is used to populate the form. So in this example, the form will be populated with the $array variable.
The second change has been made to the process method being called the DemoForm. When the second argument is true, the parser will go into the get state and execute the onGet method.
The resulting form from would look like this:
The name field has been populated with the value 'Tony' and the 'red' option has been selected on the colour select element.
onSubmit
Using the modified PHP file from above, we will now add onSubmit processing to the form.
Two changes have been made to the PHP file.
The first is an onSubmit method has been added to the DemoForm class. This is called after the form has been submitted and no validation errors were encountered. You would probably insert the post data to a database here. However, for this example, we are simply printing the post data out.
The first argument of the process method has been changed to 'submit'. This means that the parser will go into submit action when the variable $_POST[submit] is set.
Now when you submit the form, you will see the posted values printed out.