Understanding Pieces and Scripts

From FacileWiki

When creating forms with FacileForms you will come across two programming languages: PHP and Javascript. Two separate languages are required because that is the way the Internet's underlying Client/Server mechanism works - one language for the Server or website (PHP), and one language for the Client or web browser (Javascript).

In FacileForms, PHP code is called a Piece, and Javascript code is called a Script.

PHP only comes into play when generating, preparing and tweaking a form when it is first displayed in the client browser, and doesn't have any further interaction with the user until the form is "submitted", i.e. sent back to the server.

Javascript can also help display/modify the form, but unlike PHP, it is truly interactive with the user. This means that "scripts" can alter, check, hide, calculate and move elements on the form, or the form itself, after the form has been displayed and can interact with the user. The final thing javascript can do is submit the form. Once the form has been submitted, PHP takes over again until a new form or webpage is sent to the user's web browser. Finally, since only PHP runs on the server, only PHP can interact with the SQL database.

It can get a bit confusing as some (not all) actions may be done in either PHP or Javascript, for example, setting a text field value to some default text. It gets more confusing when you start embedding one type of code in another, for example, inserting PHP code into a script like this: <?php ...... ?>.

Pieces (PHP code)

In FacileForms, Pieces are divided into two sections: Form pieces and Submit pieces. Form Pieces are always executed whenever a form is called, that is, whenever a form displayed in the user's web browser. Submit pieces are only executed when a form is submitted. If a form is "abandoned" (i.e. the user clicks on a Cancel button or a weblink on the form page), the Submit piece will not be exectued.

Form Pieces are further divided into two sections: Before Form and After Form.

The Before Form piece is executed before the form is created and so before it appears in the user's browser. It is typically used to get things ready for display like reading data from the database, declaring global variables for later use by javascript, etc.

The After Form piece is executed after the form is created and sent to the user's browser and lets the server do something once the form is finally displayed.


Submit Pieces are further divided into two sections: Before Submit and After Submit.

The Before Submit piece is executed before the standard routines for saving input to the database and sending mails are run, and is typically used to check/format form data.

The After Submit piece is executed at the end of the submit code and is run after saving / mailing the input data, and is typically used to redirect the user to another form or webpage.

Important: The "Form" pieces (creating and initial displaying of the form to the client browser) and the "Submit" pieces (server code invoked by the submit action) are two independent execution processes and hence a global variable in one is not visible in the other.

Scripts (Javascript code)

These can be run anywhere, either associated with an element or in the form's own general "script" area.

When associated with an element, that is, the script is placed in the element's own "Script" section, then the script's execution is conditional. For example, if a script is placed in a text entry element's code section, then you could set it to only run if the user enters something in that element (useful for checking user-supplied data, like ensuring a numerical field only contains numbers).

When placed on the form's own general Script tab, the code will always be executed. Scripts here are divided into two sections: Initialization script and Submitted script.

The Initialization script is always executed and is typically used to set general form conditions like default values, setting elements to specific states, etc.

The Submitted script is only executed if the form is submitted and is typically used to call another form or webpage.

Note that Javascript can read globally-declared PHP ("Form" piece) code variables as follows:

    var jsvar_x =  <?php global $var_x; return $var_x; ?>

(where $var_x is a globally declared PHP variable in the Before Form piece).

That's about it. Just look at a few examples to see how variables and form elements are used.

| Terms of Use | Privacy | Sitemap |
Editing tools
Personal tools