|
Cookbook /
ProcessFormSummary: Maintain values in fields and make PVs of the form name/values when a form is submitted
Version: 1.0
Prerequisites:
Status: Beta
Maintainer: Peter Bowers
Categories: Forms
Questions answered by this recipe
When making a simple form (such as a data query or etc) which submits back on itself I want to make sure the input values are maintained between submissions. It would also be nice to have access to those values in the rest of the page through PVs. DescriptionAdd "processform" directive to maintain values between form submissions and create PVs from field values. The directive InstallationSimply copy this text into your local/config.php:
# This markup allows forms to automatically update $InputVals (to keep the same
# values displayed on the form between submissions) and $FmtPV (to allow use
# of those values in the rest of the page). Argument can be "POST" for method
# post or anything else (nothing included) for get method.
Markup('ProcessForm', '<{$var}',
'/\\(:processform\\s*(\\w*):\\)/e',
"ProcessForm(\$pagename, strtoupper('$1'))");
function ProcessForm($pagename, $method)
{
global $InputValues, $FmtPV;
if ($method == "GET")
$method_array = $_GET;
else
$method_array = $_POST;
foreach ($method_array as $k=>$v) {
$foo = htmlspecialchars($v,ENT_NOQUOTES);
# This keeps the field values current with the form from submission to
# submission, but has nothing to do with PTV
$InputValues[$k] = $foo;
# Single quotes cause havoc in pmwiki.php (line 754 eval in 2.2.0 Beta65)
# unless they are escaped with \. This line escapes any unescaped single
# quote and turns any existing escapes into double-escapes.
$foo = preg_replace('/\\\\/', '\\\\\\\\\\\\', $foo);
$foo = preg_replace("/(?<!\\\)'/", "\'", $foo);
$foo = preg_replace("/\"/", '"', $foo);
# This creates a Page Variable for use in the rest of the page
$FmtPV['$'.$k] = "'$foo'";
}
return('');
}
NOTE: See alternate code below for an important modification suggested by HansB.
Then on the page with your form simply add the That's all there is to it. NotesThis is not specific to this recipe, but if you want more instructions on how to make a query form or etc that submits onto itself, here you go...
(:input form method=GET:)
(:input hidden n {*$FullName}:)
(:input submit submit "Submit":)
If you would like to see an example that demonstrates the use of this recipe's markup then try cut/pasting this text into a page (after installing according to the above instructions), put a value in the text box, and then submit and watch what happens...
(:processform GET:)
(:input form method=GET:)
(:input hidden n {*$FullName}:)
(:input text name=mytext size=100:)
(:input submit mysubmitname "My Button Label":)
It seems like you want to talk about {$mytext}. Tell me more.
Hmmm... Just came across a function parse_str() in PHP. Looks like it might do all this job in one or 2 steps... HansB pointed out something I didn't know about forms -- they can post to arrays. Obviously my code above does not handle this, and this is the code Hans gave me as working in this situation:
foreach ($method_array as $k=>$val) {
if (is_array($val)) {
foreach ($val as $i=>$v)
$InputValues[$k][$i] = htmlspecialchars($v,ENT_NOQUOTES);
}
else $InputValues[$k] = htmlspecialchars($val,ENT_NOQUOTES);
}
I don't have time right at this point to merge this code up into mine and test it so I'm assuming if you need arrays then you can figure out what needs to be merged to make this work. (NOTE: If somebody else wants to merge it in and test it and then modify this page I am not picky about "ownership"...) (Hans:) The snippet above will set $InputValues, but not the foreach ($method_array as $k=>$v) {
if (!is_array($v))
$foo = htmlspecialchars($v,ENT_NOQUOTES);
Release Notes
See AlsoHttpVariables for another way to get access to the $_GET/$_POST values from within your page. InputDefault -- see ContributorsCookbook.InputDefault gives largely the same solution, but on a per-page basis. Comments |