Women in Technology

Hear us Roar



Article:
  Autofilled PHP Forms
Subject:   Problem with using array input names
Date:   2006-07-07 16:12:06
From:   GavinAndresen
Response to: Problem with using array input names

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).

Full Threads Newest First

Showing messages 1 through 1 of 1.

  • 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