Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

InputDefault

Summary: Demonstrates various ways to set the default values for form controls
Version:
Prerequisites:
Status:in-core
Maintainer:
Categories: Forms

<< | 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:)
(: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 color red:)
(:input select color white:)
(:input select color green:)
(:input select color red:)
(:input select color 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 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.

Comments

This is a great start! But there needs to be a way to specify multiple values for groups of checkboxes and select-multiple drop-down menus. Suggested syntax:

 (:input default favorites PmWiki,PHP,Apache:)

Ben Stallings January 22, 2007, at 09:25 AM


It is necessary to put the default form before the non-default form entries it refers to.

BenoitDutilleul? April 2, 2007, at 23:54 AM


It is also necessary to put a line break after the default tag.

Ben Stallings June 09, 2007, at 05:49 PM


Q: How to set a default from a PTV beeing on the same page?
I use the ZAP-formprocessor to make an input select:

My script:

(:zapform:)
||Summary:  ||(:input text "Summary" '{$:Summary}':)||
||Category: ||(:input select Category "Instruction":) (:input select Category "Information":) ||
(:input submit value="Save":)
(:input hidden savedata "Summary,Category":)
(:zapend:)

Summary: {$Summary}
Category: {$:Category}

So far it works. I'm able to set the values, they are stored on the same page. And when I open the page again the value "Summary" will be preset to what I typed in.
But how can I preset the input select Category to "Information"? The "default source=" works only if the variable comes from a different page.

Thanks

Andreas September, 10 2010

Can you reproduce the problem without using ZAP and re-post the form source so we can try to figure it out without having to install/learn zap?

For instance, this source here works using the current page as the source for the defaults (note how I've named the fields):

(:input form:)
(:input default source={$FullName}:)
||Summary:  ||(:input text "Summary" '{$:Summary}':)||
||Category: ||(:input select $:Category "Instruction":) (:input select $:Category "Information":) ||

Summary:something I wrote
Category:Instruction

I'm not sure how that would work with ZAP, but that's how you default from PTVs in pmwiki forms...

Peter Bowers September 10, 2010, at 06:23 AM

Hi Peter,

without ZAP and if I set the PTVs in front of the input form it works!

Summary:{$:Summary}

Category:Information

(:input form:)
(:input default source={$FullName}:)
||Summary:  ||(:input text $:Summary {$:Summary}:)||
||Category: ||(:input select $:Category "Instruction":) (:input select $:Category "Information":)||

But if I set the PTVs invisible (:Category:Information:) or I set them behind the input form it don't work.... And, by the way, how do I set PTVs without using ZAP?

Andreas - September, 13 2010

Please refer to Test.InputDefault2 where I have taken your form, changed the way you define your text field (removing the value {$:Summary} there), added a $:ThirdPTV to the form, defined $:Summary and $:Category as hidden PTVs *before* the form and $:ThirdPTV as hidden PTV *after* the form.

Reading a PTV value is absolutely agnostic in terms of (a) what format it is defined with (hidden, definition, etc.) and (b) where the PTV is defined in the text.

ZAP is a recipe that apparently has a lot of power and still apparently has a small but loyal user base. However, it is rarely (if ever) updated and questions referring to ZAP are not generally answered promptly, if at all. I would hesitate to say it has been abandoned or is unsupported, but it definitely appears to be moving that way. There are other form processors out there such as Fox, PmForm, and WikiSh. All of these will receive much more prompt answers and I believe all can handle creating PTVs from a form. (I'm not certain re PmForm because I've never gotten over the lack of documentation and so I've never used it.) Probably fox is what you are looking for.

I am leaving this discussion here on this page so you will find it, but it has gotten too long and involved for a main documentation page. Perhaps you could move it to InputDefault-Talk? and we can continue there.

Peter Bowers September 13, 2010, at 08:22 AM

Edit - History - Print - Recent Changes - Search
Page last modified on September 10, 2011, at 11:42 AM