01032: Enable processing of arrays as input values

Summary: Enable processing of arrays as input values
Created: 2008-07-04 12:33
Status: Closed, added for 2.2.76
Category: Feature
From: HansB
Assigned:
Priority: 55441
Version: 2.2 beta65
OS:

Description: sometimes it is necessary to use form input controls which are named as elements in an array, like target[0], target[1], target[2], or in select boxes. The InputDefault function does not process these, and throws php warnings.

I suggest a code change inside the InputDefault function to this snippet:

if (@$args['request']) {
  $req = array_merge($_GET, $_POST);
    foreach($req as $k => $v) {
      if (is_array($v)) {
        foreach($v as $kk => $vv)
          if (!isset($InputValues[$k][$kk])) 
            $InputValues[$k][$kk] = htmlspecialchars(stripmagic($vv), ENT_NOQUOTES); 
      }
      else if (!isset($InputValues[$k]))
        $InputValues[$k] = htmlspecialchars(stripmagic($v), ENT_NOQUOTES);
   }
}

Similarly the RequestArgs function in forms.php does not process arrays. Would be nice to have that fixed as well. I suggest a code change inside the RequestArgs function to this snippet:

if (is_null($req)) $req = array_merge($_GET, $_POST);
foreach ($req as $k=>$v) {
  if(is_array($v))
    foreach($v as $kk=>$vv)
      $req[$k][$kk] = str_replace("\r",'',stripmagic($vv));
  else $req[$k] = str_replace("\r",'',stripmagic($v));
}
return $req;

I have implemented such a feature and it is now in Subversion, also enabled on pmwiki.org. Please test it thoroughly, both the input values as arrays, and the previously working string values, and report for any problems. --Petko May 16, 2015, at 04:19 AM

(:input form method=post action={$PageUrl}:)
(:input default request=1 source={$FullName}:)
(:input default color[] red gray yellow brown:)
(:input default t2 red gray yellow brown:)
(:input default t3 "red gray yellow brown":)
(:input default s1[] value3 value4:)

Colors: (:input hidden color[] nothing:)
(:input checkbox color[] white White:)
(:input checkbox color[] red Red:)
(:input checkbox color[] green Green:)
(:input checkbox color[] blue Blue:)
(:input checkbox color[] yellow Yellow:)
(:input checkbox color[] ping Pink:)
(:input checkbox color[] black Black:)
(:input checkbox color[] gray Gray:)

summary: (:input text $:Summary size=55:)

T2: (:input text t2:)\\
T3: (:input text t3:)

Select: 
(:input select s1[] value1 size=5 multiple=multiple:)
(:input select s1[] value2:)
(:input select s1[] value3:)
(:input select s1[] value4:)
(:input select s1[] value5:)

(:input submit:)
(:input end:)

Colors:

summary:

T2:
T3:

Select:

Note how you can set default values as arrays: (:input default color[] red gray yellow brown:) Also note that for the T2 text field, only the first element of the array is used, compare with T3. Also note that when all checkboxes are unchecked, browsers don't post empty keys, so we can add a hidden element with the same name (:input hidden color[] nothing:) which is always sent. --Petko May 16, 2015, at 04:19 AM

Hi Petko, I am getting warnings on submission, when using array elements, with the latest subversion installed on my local machine (PHP 5.5.9):
Warning: htmlspecialchars() expects parameter 1 to be string, array given in G:\www\_wikis\test\pmwiki.php on line 448
I guess your changes are not published in the latest pre-release pmwiki-latest-svn.zip (I used that)? HansB May 16, 2015, at 04:52 AM

Latest SVN exported to pmwiki-latest-svn.zip. Thanks! --Petko May 16, 2015, at 06:01 AM