Tyler Lazenby
Tyler Lazenby

Reputation: 451

Dynamically Created Inputs using Name to Array are not being passed to $_POST

I cannot figure out why an input that is generated by jQuery isn't being passed to $_POST. I have three columns, generated by three functions, all called by a function that puts them all together and then appends them into the form before footer element that my buttons are contained in. The footer is part of the form, and elements that are in the 3rd column are being passed correctly. The input element generated in PHP (meaning when the page first loads up) is grabbed by PHP as well. Essentially my issue is that even though names are attached to each input (and I have made sure names are unique and using name to array functions) the inputs aren't even being passed to the $_POST even though inputs that come after these elements DO get passed that are generated dynamically in the third column.

PHP

<!----------PHP CODE THAT GETS PASSED---------->
$franchise = [];
$franchise_array = ($_POST['franchise']);
$locations = $franchise_array['location'];
// processing and sending to db code here
// Skipping to example working input that is created by PHP when page
// first loads

/* 
 * This is inside a loop where $locationCount is a variable that counts through all
 * the "locations" and generates nearly identical input groups like this one only it changes
 * which location it is posting for.
 */
<label for="location_<?php echo $locationCount; ?>_name">Location Name</label>
<input id="location_<?php echo $locationCount; ?>_name" class="locationName"
       data-targetTab="*[data-tabID=<?php echo $locationCount; ?>]" type="text"
       name="franchise[location][<?php echo $locationCount; ?>][location_name]"
       value="<?php echo h($loc['name']); ?>" // persistant input for failed validation.
/>
// Validation Error Display
<?php
if (isset($errors['location'][$locationCount]['location_name'])) {
    echo display_one_error($errors['location'][$locationCount]['location_name']);
}
?>

DOM ELEMENTS SEEN

First column first location

input id="location_1_name" class="locationName" data-targettab="*[data-tabID=1]" name="franchise[location][1][location_name]" value="" type="text"

First Column NEW location

input id="location_2_name" class="locationName" data-targettab="*[data-tabID=2]" name="franchise[location][2][location_name]" type="text"

Third Column NEW location

input id="location_2_website_url" placeholder="https://" name="franchise[location][2][primary_url}" type="url"

And finally for what I am seeing when I try to look into the $_POST array.

Array (
[franchise_name] => 
[franchise_status] => Active
[franchise_number_of_territories] => 1
[operator_id] => 
[state_owner_id] => 4
[franchise_description] => 
[franchise] => Array
    (
        [location] => Array
            (
                [1] => Array
                    (
                        [location_name] => 
                        [location_address] => 
                        [primary_city] => Array
                            (
                                [city_name] => 
                            )

                        [location_country] => United States
                        [location_state] => Alabama
                        [location_zip] => 
                        [phone] => Array
                            (
                                [1] => Array
                                    (
                                        [desc] => 
                                        [number] => 
                                    )

                                [2] => Array
                                    (
                                        [desc] => 
                                        [number] => 
                                    )

                            )

                        [location_map_code] => 
                        [primaryURL] => 
                        [location_analytics_id] => 
                        [url] => Array
                            (
                                [1] => Array
                                    (
                                        [type] => Facebook
                                        [address] => 
                                    )

                                [2] => Array
                                    (
                                        [type] => YouTube
                                        [address] => 
                                    )

                            )

                    )

                [2] => Array
                    (
                        [googleAnalytics] => 
                        [url] => Array
                            (
                                [1] => Array
                                    (
                                        [type] => Facebook
                                        [address] => 
                                    )

                                [2] => Array
                                    (
                                        [type] => YouTube
                                        [address] => 
                                    )

                            )

Upvotes: 0

Views: 200

Answers (1)

Tyler Lazenby
Tyler Lazenby

Reputation: 451

The DOM element for the third column is showing has an curly brace instead of a bracket at the end of the input name

name="franchise[location][2][primary_url}"

Changed to

name="franchise[location][2][primary_url]"

Works

Upvotes: 0

Related Questions