ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

PHP:Form Series, Part 2: Formtypes & Setting Default Values

(Page 2 out of 3)

In the previous examples, I used inline form types. The form types were included in the form HTML. But you can also use external form types. This allows you to create a certain form style, and use this style in different forms.

An external form type must follow a certain format, which looks like this:




                                       
                ]]>
       


                This isn't even a {var:bla}
]]>
       

(View live external form type)

This is very simple XML, and shouldn't be hard to understand.

You must also link the external form type in your form, using the src attribute on a formtype tag, like so:

"example">
"external.xml" />

        "mytype" label="Name:" name="name" />
        "mytype" label="Email:" name="email" />

        "submit" value="Go!" />

(View live demo)

The src must be a path to your external form type file, e.g. /home/you/public_html/external.xml or simply external.xml if the form type file is in the same directory as your form.

External form types give you tremendous power, and can be used to give all your forms a slick look, without having to write much HTML at all. Just write it once, and be done with it!

Two special form types
There are two special form types called 'begin' and 'end' (e.g. ). These can be used to control the beginning and ending of your form. See the demo below for a working example:

"example">
        "mytype">
        [CDATA[
        "text" name="{var:name}" />
        ]]>
       

        "begin">
        [CDATA[
       

"{var:name}" method="POST" action="">
       

Custom Form Beginning!


        ]]>
       

        "end">
        [CDATA[
       
        ]]>
       

        "mytype" label="Name:" name="name" />
        "mytype" label="Email:" name="email" />

        "submit" value="Go!" />

(View live demo)

When using a custom form begin or end, you mustn't forget to include

or
. If you forget this, your form will most likely not work, and could cause errors.

I hope you now understand what form types are, and how powerful they are. They can really make your life easier, especially when using external form types. Just link 'em, and use 'em! Let's now have a look at my favorite feature: setting default values.

« Previous: Introduction
Next: Setting Default Values »



Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction
  2. Different kinds of form types
  3. Setting Default Values
Bookmark Article
Download Article
PDF
Download this article as a PDF file