سؤال

I have a form element and I would like the URL to be

http://example.com/page.html?a=constant+QUERY_TEXT

If I wanted

http://example.com/page.html?a=constant&b=QUERY_TEXT

I could use

<form action="http://example.com/page.html">
  <input type="hidden" name="a" value="constant">
  <input type="text" name="b">
  <input type="submit">
</form>

but is there any way to get my desired form without scripting?

هل كانت مفيدة؟

المحلول

You could create a server proxy to do it for you. Put a handler on your server that redirects requests to the target server and adds the constant to the querystring. Depending on your web server, it seems like you should be able to do this with just configuration settings, no code required.

<form action="/myproxy">
  <input type="text" name="a">
  <input type="submit">
</form>

Apache mod_alias RedirectMatch directive:

RedirectMatch ^/myproxy?a=(.+)$ http://example.com/page.html?a=constant+$1

IIS URL Rewrite Module:

<rewrite>
  <rules>
    <rule name="proxyRedirect" stopProcessing="true">
      <match url="^myproxy?a=(.+)$"/>
      <action type="Redirect" url="http://example.com/page.html?a=constant+{R:1}"/>
    </rule>
  </rules>
</rewrite>

There is no way to do it with just HTML and no JavaScript.

نصائح أخرى

What exactly are you posting the form too? What language are you using? I guess I don't understand fully what you are trying to do, because both of these examples:

http://example.com/page.html?a=constant+QUERY_TEXT
http://example.com/page.html?a=constant&b=QUERY_TEXT

Could work, and be parsed by whatever language is receiving the form. It would only take a bit of logic to deconstruct the string. I've done something similar using jQuery and Ajax for my form submissions.

I have a simple button that holds several key parameters like so:

<a class="alertDeleteButton" href="https://www.example.com/index.cfm?controller=hobbygroupmember&amp;action=deletemember&amp;deleteuserid=#userid#&amp;hobbyid=#id#&amp;adminid=#user.key()#" id="alertDeleteButton_#id#_#userid#" title="#deleteMemberText#">DELETE</a>

As you can see the href holds all kinds of information (dynamic and static) that I want to pass to my function. Then with jQuery I do this:

 $(".alertDeleteButton").click(function(e) {

   var removeButtonString = $(this).attr("href");
   var parsed_string = $.parseQuery(removeButtonString);
   var currentArgument = $.parseQuery(removeButtonString).currentArgument;
   var hobbyid = $.parseQuery(removeButtonString).hobbyid;
   ......

With jQuery I get all the fields I need, and then just pass them with an ajax call.

Is that what you are looking to do?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top