InputDefaults


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

(:input default ... :)

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:)
(:input checkbox color white:) White
(:input checkbox color green:) Green
(:input checkbox color red:) Red
(:input checkbox color blue:) Blue

White Green Red Blue

$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, this page has the following local customization enabled in local/Test.InputDefaults.php:

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

As a result, any values from a previous form submission are used to set the defaults of the controls. 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:)

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:

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 defaults source=Cookbook.Fox:)
(:input textarea name=$:Summary cols=50 rows=5 :)

 0: 00.00 00.00 config start
 1: 00.01 00.01 config end
 2: 00.21 00.20 MarkupToHTML begin
 3: 00.21 00.20 MarkupToHTML begin
 4: 00.21 00.21 MarkupToHTML end
 5: 00.21 00.21 MarkupToHTML begin
 6: 00.21 00.21 MarkupToHTML end
 7: 00.21 00.21 MarkupToHTML begin
 8: 00.22 00.21 MarkupToHTML end
 9: 00.22 00.21 MarkupToHTML begin
10: 00.22 00.21 MarkupToHTML end
11: 00.25 00.24 MarkupToHTML end
12: 00.25 00.24 MarkupToHTML begin
13: 00.26 00.25 ReadApprovedUrls SiteAdmin.ApprovedUrls begin
14: 00.26 00.25 ReadApprovedUrls SiteAdmin.ApprovedUrls end
15: 00.27 00.26 MarkupToHTML end
16: 00.27 00.26 MarkupToHTML begin
17: 00.27 00.26 MarkupToHTML end
18: 00.27 00.26 now
Peak memory: 3,917,592 bytes