Women in Technology

Hear us Roar



Article:
  Autofilled PHP Forms
Subject:   Problem with using array input names
Date:   2006-07-06 23:47:19
From:   sdave1284
Hey great script! This will save so much time. Now I just have to go back and get all my old scripts to use it. I did run into one problem and I wanted to see if you may have a solution....


If you have input fields like the following:


<input type="text" name="phone1[1]" id="phone1[1]">-
<input type="text" name="phone2[1]" id="phone2[1]">-
<input type="text" name="phone3[1]" id="phone3[1]">


It will not fill in the values. I guess it doesnt recognize how to fill in an array of values with <? echo $_POST['phone1'][1] ?>, etc....


Any ideas how I could modify the script or if its possible??

Full Threads Newest First

Showing messages 1 through 2 of 2.

  • Problem with using array input names
    2006-07-07 16:12:06  GavinAndresen [View]

    I was wondering when somebody would notice that it doesn't handle PHP's built-in support for arrays of form fields.

    On a scale of 1-10, that will be about a '6' to fix; it will involve changes to the fillInInputTag() and fillInTextArea() (and maybe fillInSelect) functions, where they fetch the "name" attribute (gotta be smarter about name="foo[]", and keep track of which array index is being parsed, and find the corresponding value in $request).
    • Problem with using array input names
      2006-07-11 00:21:32  sdave1284 [View]

      Well, I don't use it everywhere. I only use it for text fields. So I made the following addition to fillInInputTag()(Note: i'm not very experienced with the best solutions to these problems, nor do I use regular expressions often):

      $num_of_matches = preg_match("/([\w]+)(\W([\d]+)\W)/", $name, $name_match);

      $name_arr = $name_match[1];
      $name_index =$name_match[3];

      if($num_of_matches == 0) {
      if (!array_key_exists($name, $this->request))
      return $tag;
      return fillInFormHelper::replaceAttributeVal($tag, 'value', $this->request[$name]);
      } else {
      if (!array_key_exists($name_arr, $this->request))
      return $tag;

      return fillInFormHelper::replaceAttributeVal($tag, 'value', $this->request[$name_arr][$name_index]);
      }

      In my input names, I add a index number instead of just using name[]. I think this gives me better control, plus I can use the above method to fill in the text fields. Now, I just need to figure out how to do the same thing for the validateForm function....

      Your scripts have made me life creating version 2 of our website 100X easier. Expansion will be much easier as new fields are added to the forms and database backend! Thanks and feel free to comment on what I did above...

      Dave