Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 解析表单(POST)数据以创建多维哈希_Html_Perl_Hash_Dancer - Fatal编程技术网

Html 解析表单(POST)数据以创建多维哈希

Html 解析表单(POST)数据以创建多维哈希,html,perl,hash,dancer,Html,Perl,Hash,Dancer,我有一个表单,用于提交多人的数据。每个人都有多个属性,我将它们分组如下: <input type=hidden name="person1[firstname]" value='Sam'/> <input type=hidden name="person1[lastname]" value='Higgins'/> <input type=hidden name="person2[firstname]" value='Jiminy'/> <input t

我有一个表单,用于提交多人的数据。每个人都有多个属性,我将它们分组如下:

<input type=hidden name="person1[firstname]" value='Sam'/>
<input type=hidden name="person1[lastname]" value='Higgins'/>

<input type=hidden name="person2[firstname]" value='Jiminy'/>
<input type=hidden name="person2[lastname]" value='Cricket'/>

...etc
my %hash = params;
die Dumper \%hash;
我得到:

VAR1 = {
          'person1[firstname]' => 'Sam',
          'person1[lastname]' => 'Higgins',
          'person2[firstname]' => 'Jiminy',
          'person2[lastname]' => 'Cricket',
};
当我期待像这样的事情时:

VAR1 = {
          'person1' => { firstname => 'Sam', lastname => 'Higgens' },
          'person2' => { firstname => 'Jiminy', lastname => 'Cricket' },
};
有没有办法获得上述信息,或者我在HTML中做得不对

编辑

我也尝试过在结尾使用空括号:

<input type=hidden name="person1[firstname][]" value='Sam'/>

要比提供链接更全面地回答问题,请执行以下操作:

我使用了一个使用jQuery的解决方案,特别是使用插件()异步发送数据(AJAX),并使用Dancers
from_json
方法创建json字符串的hashref

我之所以强调字符串,是因为serializeJSON插件提供的函数创建了一个JSON对象,而这个舞者没有转换成正确的结构。因此,您需要使用
JSON.stringify()
创建一个JSON字符串,Dancer不接受该字符串:)

代码如下:

HTML示例:

<input type=hidden name="person1[firstname]" value='Sam'/>
<input type=hidden name="person1[lastname]" value='Higgins'/>
Perl(舞者):


感谢所有帮助过我的人

通过输入姓名
person[1][firstname]
person[1][lastname]
,您可以在表单中让自己的生活更加轻松。。。或者不是,因为Dancer似乎不会从输入中创建多维结构。嗯,我尝试通过将
JSON
转换为JSON字符串来使用它,但这仍然给了我一个键,因为
person1[firstname]
我希望不走这条路,但似乎我必须用perl正则表达式来创建我自己的数据结构,就像用来编码表单数据的那样。这可能会使您不必创建数据结构@我最终使用了ialarmedalien,因为formToObject给了我只读元素的类型错误。我真的很感激他们提出了其他类似的解决方案,非常无私,那就是格雷特汉克斯·卢克,然而我提出了一个解决方案,我不需要自己制作结构
#!/usr/bin/perl
use Data::Dumper;
my $orginal = {
          'person1[firstname]' => 'Sam',
          'person1[lastname]' => 'Higgins',
          'person2[firstname]' => 'Jiminy',
          'person2[lastname]' => 'Cricket',
};  

my $result = {};
foreach my $key (keys %$orginal)
{
    $value = $orginal->{$key};
     $key =~ m/^(.*)\[(.*)\]$/;

     #$1 = for example person1
     #$2 = forexample firstname
     $result->{$1}->{$2} = $value;


}

print Dumper($result);
#RESULT:

# $VAR1 = {
#           'person1' => {
#                          'firstname' => 'Sam',
#                          'lastname' => 'Higgins'
#                        },
#           'person2' => {
#                          'firstname' => 'Jiminy',
#                          'lastname' => 'Cricket'
#                        }
#         };
<input type=hidden name="person1[firstname]" value='Sam'/>
<input type=hidden name="person1[lastname]" value='Higgins'/>
var formData = $(this).serializeJSON();
console.log(formData);
$.ajax({
     'url': '/your_url',
     'type': 'POST',
     'data': JSON.stringify(formData),
     'success': function(res){
         console.log(res);
   }
 });
post '/your_url' => sub {
    my $json = request->body;
    use Data::Dumper;
    my $hashref = {};
    $hashref = from_json($json);
    die Dumper \$hashref->{person1}->{name}; # 'Sam'
}