InputDefault

Summary: Demonstrates various ways to set the default values for form controls
Version:
Prerequisites:
Status:in-core
Maintainer: Petko
Categories: Forms, MarkupOnly, PHP72
Discussion: InputDefault-Talk

<< | Input | >>

This page demonstrates various ways to set the default values for form controls.

(:input default ... :) (:input defaults ... :)

The (:input default name value:) markup allows an author to set the default value for any input control with the corresponding name. For example, the following markup sets the default value of the "rating" radio buttons to "medium":

(:input default rating medium:)
(:input radio rating none:) None
(:input radio rating low:) Low
(:input radio rating medium:) Medium
(:input radio rating high:) High
(:input radio rating extreme:) Extreme

None Low Medium High Extreme

Of course, (:input default ...:) works for any control, not just radio buttons:

(:input default color red blue:)
(:input checkbox color white:) White
(:input checkbox color green:) Green
(:input checkbox color red:) Red
(:input checkbox color blue:) Blue

White Green Red Blue

(:input default color2 red:)
(:input select color2 white:)
(:input select color2 green:)
(:input select color2 red:)
(:input select color2 blue:)

The (:input default ...:) directive works with textareas, provided there are no line breaks in the value:

(:input form:)
(:input default area "This is the value of a text area.":)
(:input textarea area rows=2 cols=40:)
(:input end:)

But you can set multi-line defaults for textarea controls using the source= parameter and PTVs. See below. You can also set multi-line defaults for textareas using [=:

(:input form:)
(:input default area2 [=This is the 
multi-line value 
of a text area.=]:)
(:input textarea area2 rows=3 cols=40:)
(:input end:)

(:input default request=1 :)

The request=1 parameter will result in setting the defaults of the controls with any values from a previous form submission. Try setting the form below, press "Go", and notice how the form controls retain their values even after submit is pressed:

(:input form method=get:)
(:input default request=1:)
Colors: (:input checkbox red 1:) Red (:input checkbox blue 1:) Blue (:input checkbox green 1:) Green
Extras: (:input radio extras yes:) Yes (:input radio extras no:) No
Name: (:input text name:)
Year: (:input select year 2006:) (:input select year 2007:) (:input select year 2008:) 

(:input submit:)
(:input end:)
Colors: Red Blue Green
Extras: Yes No
Name:
Year:

Note: The same can be achieved by setting the $InputValues from a local config script. See For Developers below.

The (:input default ...:) directive above is simply an easy way to use markup to set entries in $InputValues. However, the (:input default:) directive won't replace any value that already exists (thus a form submission will take priority over the markup defaults).

(:input default source=... :)

The source=Group.PageName parameter can be used to set the defaults of controls from PageTextVariables (PTVs) on another page (the source page). For this set the names of the controls to be the PTV names, for instance (:input text name=$:Town :) to set the default from a PTV Town.

You can set multiple pages as sources: (:input default source=Page1,Page2,Page3:)? In that case, the first existing page where the user has read permissions, will be used to populate the form fields.

You can set a multi-line default value in a (:input textarea ..:) control from a PTV on a source page by setting (:input default source=SourcePageName :) with the source pagename and setting the textarea input control as name=$:PTV. The following example gets a PTV called 'Message' from page Test.MultipleLinePTV

(:input default source=Test.MultipleLinePTV:)
(:input textarea name=$:Message cols=50 rows=6 :)

"defaults" is a synonym of "default".

For Developers: Use of $InputValues

From within a script, the $InputValues array can also be used to set default values for form controls. These can come from any source -- including from a previous POST or GET operation. For example to enable forms to retain previous input, after unsuccessful submissions, set in a local config file:

foreach ($_GET as $k=>$v) 
   $InputValues[$k] = htmlspecialchars($v);

The Test.InputDefaults page has the $InputValues array enabled so you can see how it works.

See also

Contributors

Comments

See discussion at InputDefault-Talk