使用“名称到数组”的动态创建的输入不会传递到$ _POST

时间:2018-08-09 15:36:14

标签: javascript php html arrays forms

我不知道为什么jQuery生成的输入没有传递给$ _POST。我有三列,由三个函数生成,所有列均由一个函数调用,该函数将它们全部放在一起,然后将它们附加到包含我的按钮的页脚元素之前的表单中。页脚是表单的一部分,元素在表单中第三列已正确传递。 PHP也会捕获用PHP生成的input元素(意味着页面首次加载时)。本质上,我的问题是,即使每个输入都附加了名称(并且我已经确保名称是唯一的,并且使用名称对数组函数进行了命名),即使这些元素后面的输入都没有将输入传递给$ _POST,会在第三栏中动态生成。

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元素

第一列第一位置

  

输入id =“ location_1_name” class =“ locationName” data-targettab =“ * [data-tabID = 1]” name =“ franchise [location] [1] [location_name]” value =“” type =“ text “

第一列的新位置

  

输入id =“ location_2_name” class =“ locationName” data-targettab =“ * [data-tabID = 2]” name =“ franchise [location] [2] [location_name]” type =“ text”

第三列新位置

  

输入id =“ location_2_website_url”占位符=“ https://” name =“ franchise [location] [2] [primary_url}” type =“ url”

最后,我尝试查看$ _POST数组时所看到的内容。

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] => 
                                    )

                            )

1 个答案:

答案 0 :(得分:0)

正在显示的第三列DOM元素在输入名称的末尾有一个大括号而不是括号

  

name =“ franchise [location] [2] [primary_url}”

更改为

  

name =“ franchise [location] [2] [primary_url]”

作品