开发者

Submitting an HTML form with a missing parameter name

开发者 https://www.devze.com 2023-01-07 20:45 出处:网络
Normally an HTML form sends query parameters as key-value pairs like this: http://blabla/?something=something&this=that

Normally an HTML form sends query parameters as key-value pairs like this:

http://blabla/?something=something&this=that

But what I need is a form that generates a URL with one of the query keys omitted:

http://blabla/?something&this=that

As far as I can tell, a missing or empty name attribute does not quite provide what I expect:

<input type="hidden" name="" value="myvalue"/>

Leads to this, with an equals sign that I don't want:

http://blabla/?=myvalue

I know it's not good practice to do this, but I need to开发者_运维知识库 interface with an existing poorly-designed system.


If you need the attribute to not have a value, shouldn't you do something like this instead?

<input type="hidden" name="something" value=""/>

which would produce the URL http://blabla/?something=&this=that that you are looking for, only with the '=' after something. Or, just leave it out entirely (ie, do not define an input type hidden) and you would get the URLhttp://blabla/?this=that ...


Maybe I'm missing the point here, but either just don't submit that value or set it to null prior to submitting the form. It's not good practice to have an input without a name, so keep the name.

Obviously, we don't know how the script that accepts the form input is setup, but in my experiences unless some sort of crazy server-side validation was setup, it shouldn't bark at you.


These answers make sense logically, but unfortunately this system is very picky about which characters it will accept and any spurious equals signs give it trouble. It's an Innovative Interfaces library OPAC, by the way.

I figured out one way to do it, which is by not submitting the form at all but using JavaScript to inject the contents of the text box into a dynamically-generated URL and then opening that using window.location:

<form name="search_form" method="get" action="">
    <input type="text" size="30" maxlength="100"/>
    <input type="submit" value="Search" />
</form>

<script type="text/javascript">
window.onload = function() {
    document.search_form.onsubmit = function() {
        var term = document.search_form.elements[0].value;
        var url = "http://blabla/search/X?d:(electronic books) and ("
            + term + ")&searchscope=1";
        window.location = url;
    }
}
</script>

I don't do much JavaScript and this will certainly cause alarm to anyone mindful of accessibility and web standards compliance. However, rest assured it is no worse than any of the rest of the javascriptaghetti that is part of this system.

0

精彩评论

暂无评论...
验证码 换一张
取 消