简体   繁体   English

具有相同“名称”属性的多个表单字段未发布

[英]Multiple form fields with same 'name' attribute not posting

I'm dealing with some legacy HTML/JavaScript. 我正在处理一些旧的HTML / JavaScript。 Some of which I have control over, some of which is generated from a place over which I have no control. 其中一些我可以控制,有些是从我无法控制的地方生成的。

There is a dynamically generated form with hidden fields. 有一个动态生成的带有隐藏字段的表单。 The form itself is generated via a Velocity template (Percussion Rhythmyx CMS) and JavaScript inserts additional hidden form fields. 表单本身是通过Velocity模板(Percussion Rhythmyx CMS)生成的,JavaScript会插入其他隐藏的表单字段。 The end result is hidden form fields generated with the same 'name' attribute. 最终结果是使用相同的“名称”属性生成的隐藏表单字段。 The data is being POSTed to Java/JSP server-side code about which I know very little. 数据正被发布到Java / JSP服务器端代码,而我对此所知甚少。

I know that form fields sharing the same 'name' attribute is valid. 我知道共享相同“名称”属性的表单字段是有效的。 For some reason the POSTed data is not being recognized the back end. 由于某种原因,后端无法识别POST数据。 When I examine the POST string, the same-name-keys all contain no data. 当我检查POST字符串时,相同名称的键都不包含任何数据。

If I manipulate the code in my dev environment such that only a single input field exists for a given name, the data IS POSTed to the back end correctly. 如果我在开发环境中操纵代码,以使给定名称仅存在一个输入字段,则数据将正确地发布到后端。 The problem is not consistent, sometimes, it works just fine. 问题并不总是一致的,有时它可以正常工作。

Is there something I can do to guarantee that the data will be POSTed? 我可以做些什么来保证将数据过帐吗? Can anyone think of a reason why it would not be? 谁能想到一个不会的原因?

I should really update my answer and post code here, because POST requests without 
variable strings indicates the problem is on the client side.

How about this: 这个怎么样:

<script type="text/JavaScript">
    function disableBlankValues()
    {
        var elements = document.getElementById("form1").elements;
        for (var i = 0; i < elements.length; i++)
        {
            if (elements[i].value == "")
                elements[i].disabled = true;
        }
    }
</script>

<form action="page.php" method="POST" onsubmit="disableBlankValues()" id="form1">
    <input type="hidden" name="field1" value="This is field 1."/>
    <input type="hidden" name="field1" value=""/>
</form>


EDIT 编辑

I now realize the actual problem (multiple variables with the same name should be passed to JSP as an array) and my solution is probably not what the OP is looking for, but I'm leaving it here just in case it happens to help someone else who stumbles upon this post. 现在,我意识到了实际的问题(应将多个具有相同名称的变量作为数组传递给JSP),而我的解决方案可能不是OP所寻找的,但我将其留在此处,以防万一碰巧能帮助某人其他偶然发现此职位的人。

you could use something like: 您可以使用类似:

var form = document.getElementById('yourformid');
var elements = form.getElementsByName('repeatedName');
var count = 0;
for(var item in elements){
   elements[item].name += count++;
}

this way you will get each hiddenfield with the names: 这样,您将获得每个隐藏字段的名称:

name0
name1
name2
...

I've worked out a brute-force solution. 我已经制定出一种蛮力的解决方案。 Note that I'm pretty aware this is a hack. 请注意,我很清楚这是一个hack。 But I'm stuck in the position of having to work around other code that I have no control over. 但是我一直处于不得不处理我无法控制的其他代码的位置。

Basically, I've created an ONSUBMIT handler which examines the form for the repeated hidden fields and makes sure they are all populated with the correct data. 基本上,我创建了一个ONSUBMIT处理程序,该处理程序检查表单中是否存在重复的隐藏字段,并确保它们都填充有正确的数据。 This seems to guarantee that the POST string contains data regardless of how the form gets rendered and the Java back end appears to be happy with it as well. 这似乎可以确保POST字符串包含数据,而不管表单的呈现方式如何,并且Java后端似乎也对此满意。

I've tested this in the following situations: 我已经在以下情况下对此进行了测试:

  1. Code generates single instances of the hidden fields (which does happen sometimes) 代码生成隐藏字段的单个实例(有时会发生)
  2. Code generates multiple instances of the hidden fields 代码生成隐藏字段的多个实例
  3. Code generates no instances of the hidden fields (which should never happen, but hey...) 代码生成没有隐藏字段的情况下(这绝不应该发生,但嘿...)

My 'else' condition contains a tiny bit of MooTools magic, but it's otherwise straight-forward stuff. 我的“其他”条件包含MooTools的一点魔力,但它本来就很简单。

Maybe someone else will find this useful one day... 也许其他人会发现这一天有用...

Thanks for the help! 谢谢您的帮助!

<form method="post" name="loginform" id="loginform" action="/login" onsubmit="buildDeviceFP(this);">
    <script type="text/javascript">
        function insertFieldValues( fields, sValue )
        {
            if ( 'length' in fields ) 
            {
                // We got a collection of form fields
                for ( var x = 0; x < fields.length; x++ ) {
                    fields[x].value = sValue;
                }
            }
            else
            {
                // We got a single form field
                fields.value = sValue;
            }
        }

        function buildDeviceFP( oForm )
        {
            // Get the element collections for Device Fingerprint & Language input fields from the form.
            var devicePrintElmts = oForm.elements.deviceprint;
            var languageElmts    = oForm.elements.language;

            // 'devicePrintElmts' & 'languageElmts' *should* always exist.  But just in case they don't...
            if ( devicePrintElmts) {
                insertFieldValues( devicePrintElmts, getFingerprint() );
            } else if ( oForm.deviceprint ) {
                oForm.deviceprint.value = getFingerprint();
            } else {
                $('logonbox').adopt(
                    new Element( 'input', {'type':'hidden', 'name':'deviceprint', 'value':getFingerprint()} )
                );
            }

            if ( languageElmts) {
                insertFieldValues( languageElmts, getLanguage() );
            } else if ( oForm.language ) {
                oForm.language.value = getLanguage();
            } else {
                $('logonbox').adopt(
                    new Element( 'input', {'type':'hidden', 'name':'language', 'value':getLanguage()} )
                );
            }
        }
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM