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

发布于 2025-01-01 18:38:38 字数 599 浏览 1 评论 0原文

我正在处理一些遗留的 HTML/JavaScript。其中一些是我可以控制的,其中一些是从我无法控制的地方生成的。

有一个带有隐藏字段的动态生成的表单。表单本身是通过 Velocity 模板 (Percussion Rhythmyx CMS) 生成的,JavaScript 会插入额外的隐藏表单字段。最终结果是使用相同的“名称”属性生成隐藏表单字段。数据被发布到 Java/JSP 服务器端代码,对此我知之甚少。

我知道共享相同“名称”属性的表单字段是有效的。由于某种原因,后端无法识别已发布的数据。 当我检查 POST 字符串时,同名键均不包含数据。

如果我在开发环境中操作代码,使得给定名称仅存在一个输入字段,则数据是正确发布到后端。问题并不一致,有时,它工作得很好。

我可以采取什么措施来保证数据将被发布?谁能想到为什么它不会的原因吗?

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

I'm dealing with some legacy 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. 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.

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. When I examine the POST string, the same-name-keys all contain no data.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗弃M 2025-01-08 18:38:38

这个怎么样:

<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

我现在意识到实际的问题(多个同名的变量应该作为数组传递给JSP),我的解决方案可能不是OP正在寻找的,但我将它留在这里,以防它碰巧帮助某人其他偶然发现这篇文章的人。

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.

九厘米的零° 2025-01-08 18:38:38

你可以使用类似的东西:

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

这样你就会得到每个隐藏字段的名称:

name0
name1
name2
...

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
...
嘿哥们儿 2025-01-08 18:38:38

我已经想出了一个暴力解决方案。请注意,我很清楚这是一个黑客行为。但我陷入了必须解决我无法控制的其他代码的境地。

基本上,我创建了一个 ONSUBMIT 处理程序,它检查表单中重复的隐藏字段,并确保它们都填充了正确的数据。这似乎保证了 POST 字符串包含数据,无论表单如何呈现,并且 Java 后端似乎也对此感到满意。

我在以下情况下对此进行了测试:

  1. 代码生成隐藏字段的单个实例(有时确实会发生)
  2. 代码生成隐藏字段的多个实例
  3. 代码不生成隐藏字段的实例(应该)永远不会发生,但是嘿...)

我的“else”条件包含一点点 MooTools 的魔力,但它在其他方面是直接的东西。

也许有一天其他人会发现这很有用...

感谢您的帮助!

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

I've worked out a brute-force solution. Note that I'm pretty aware this is a 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. 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.

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.

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文