FormStack, PHP: POST information for name and address fields as nested arrays

264 Views Asked by At

I'm trying to get FormStack to POST data to a script I'm making so it can be inserted into my own MySQL database. I'm able to get the data and parse into an array that looks something like this:

Array
(
  [FormID] => 1111111
  [UniqueID] => 222222
  [name_field1] => first = FirstName last = LastName
  [address_field1] => address = 123 Street Name address2 = Suite 1000 city = Exampleville state = WY zip = 12345
  [random_field] => random text
  [name_field2] => first = FirstName last = LastName
  [address_field2] => address = 123 Street Name address2 = Suite 1000 city = Exampleville state = WY zip = 12345
)

The problem is the way the name_fields and address_fields are formatted. I've made forms that had POST data containing multidimensional arrays before but this is formatted differently than that. I've searched and searched and played around with it and I am able to get that data into an array of it's own but it's way more code than I would like and I would have to duplicate it for every name field and every address field. I feel like I must be missing a simpler way to address those individual pieces of the name and address fields. Or is it as tedious as it seems to be?

Thanks

1

There are 1 best solutions below

0
On

This is likely not the most elegant solution, but I'm just glad I was able to come up with any solution. Here's what I came up with:

foreach ($_POST as $key => $value) {
    if (preg_match("/address2 = /", $value)) {
        $search = array("address2 = ", "city = ", "state = ", "zip = ");
        $addr_step1 = str_replace($search, "|", $value);
        $addr_step2 = str_replace("address = ", "", $addr_step1);
        $addr_step3 = explode("|", $addr_step2);
        $addr_keys = array("Address", "Address2", "City", "State", "Zip");
        $value = array_combine($addr_keys, $addr_step3);
    } elseif (preg_match("/address = /", $value)) {
        $search = array("state = ", "zip = ");
        $addr_step1 = str_replace($search, "|", $value);
        $addr_step2 = str_replace("city = ", "| |", $addr_step1);
        $addr_step3 = str_replace("address = ", "", $addr_step2);
        $addr_step4 = explode("|", $addr_step3);
        $addr_keys = array("Address", "Address2", "City", "State", "Zip");
        $value = array_combine($addr_keys, $addr_step4);
    } elseif (preg_match("/first = /", $value)) {
        $name_step1 = str_replace("last = ", "|", $value);
        $name_step2 = str_replace("first = ", "", $name_step1);
        $name_step3 = explode("|", $name_step2);
        $name_keys = array("FirstName", "LastName");
        $value = array_combine($name_keys, $name_step3);
    }
    ${'f' . $key} = $value;
}

That last bit ${'f'.$key} = $value; is to give each field a variable to address it by (I'm having FormStack use the numeric field names, which is why I'm appending the 'f' to the beginning). So for example, something like this...

echo "$f12345678[City], $f12345678[State]";

would show me...

New York, NY