Building a URL with a Form via JavaScript?
-
Anyone have a JavaScript trick to create a web form that builds a URL to send users to a pre-populated search? I'm trying to build a form that can add various variables to a URL prior to submitting, conducting a search with prepopulated criteria. For example, I'd like the web form to say:Find: [ ] Widgets [ ] Doodads [ ] Gizmos Priced: [ ] Below $5 [ ] Below $500 [ ] Below $5000The equivalent plain, non-trick HTML would look like this:<input type="checkbox" name="type" value="widgets"> <input type="checkbox" name="type" value="doodads"> <input type="checkbox" name="type" value="gizmos"> <input type="checkbox" name="maxprice" value="0005"> <input type="checkbox" name="maxprice" value="0500"> <input type="checkbox" name="maxprice" value="5000">But, instead of the form sending information the usual way, selecting the various elements would instead add, remove, or change variables within a URL to which it would send users when they clicked "Submit." For example, changing the above fields would, behind the scenes, build URLs like:http://www.search.com/query?maxprice=0005 http://www.search.com/query?product=doodads http://www.search.com/query?product=doodads&maxprice=0500Users could not select various fields without breaking the URL, but also combine any series of variables available. Clicking submit would finalize the search URL and send them straight to the results. Presume that the receiving site would understand the variable names and values sent in the URL. This seems pretty straightforward, but I can't find example code to do exactly this. Any pointers would be much appreciated!
-
Answer:
That is to say, if your plain-vanilla HTML form looks something like this: <form method="GET" action="http://www.search.com/query"> <input type="checkbox" name="type" value="widgets"> <input type="checkbox" name="type" value="doodads"> <input type="checkbox" name="type" value="gizmos"> <input type="checkbox" name="maxprice" value="0005"> <input type="checkbox" name="maxprice" value="0500"> <input type="checkbox" name="maxprice" value="5000"> <input type="submit"> </form> Then the form variables are appended to the formâs action URI when the user submits the form.
pzarquon at Ask.Metafilter.Com Visit the source
Other answers
Umm, I may be confused, but what youâre describing looks exactly like the normal form behaviour when the request method is GET.
breaks the guidelines?
I think I understand what you're saying, but... does the GET method assemble the URL on the client side and submit it? Or is it still submitting the individual variables to the search application on the remote site? Basically we're working with a custom search application that has its own forms doing things in .NET, but that the developers have built so that we can build a URL to get a search result with one click. http://www.search.com/query?product=doodads&maxprice=0500&location=us&vendorid=4159 In other words, if I clicked that link I'd get my results on their site. Apart from using the integrated search form, using URLs like that is the only way I know how to preset variables in a search from outside. I thought I'd need a Javascript code to put the pieces together, but if not, do tell! And... well, obviously, 101-level tutorial sites or example code would be much appreciated.
pzarquon
Oops. I posted my response to your one-line reply, and you've gone ahead and given me the code in the mean time. I'll try that!
pzarquon
Yes, the URI for the GET is assembled by the browser on the client side, including the form variables. However, I donât understand your question, âis it still submitting the individual variables to the search application on the remote site?â Ignoring cookies, in a GET request, the only input to the remote application is the URI. Itâs up to the remote application to interpret the meaning of any variables in the query portion of the URI.
breaks the guidelines?
It worked! Of course I was thinking immediately, "You'd think this is how forms should work in the first place." And they do. And I'm an doofus. I shudder to imagine what madness I'd descend into if I tried to recreate this with JavaScript or something even more pointless. Thanks!
pzarquon
Related Q & A:
- If we end up building a base on moon, what do you think will be a, if any, beneficial side effect for mankind?Best solution by Yahoo! Answers
- Should I study civil engineering or building engineering to become a developer?Best solution by Yahoo! Answers
- What is the difference between a bank and a building society?Best solution by Yahoo! Answers
- Building a custom sub enclosure?Best solution by wikihow.com
- Are they still going to be building a Bass Pro Shop in Bakersfield, CA?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.