views:

54

answers:

4

I have a form with some input fields (6). When I click the submit button "I would like to format the URL" in such a way:

/actionname?input1|input2|input3|input4|input5|input6

and maybe for null values: /actionname?input1|input2||input4||input6

and maybe a time-stamp for when the user clicks the submit button.

Can this be done using javascript.

Thanks,

Chad

+1  A: 

It can be done via javascript but I can't understand why you would want to. You would have to use the onsubmit event of the form, loop through the form elements appending each value to a url string in the format you offered and then setting window.location to that url string.

For users with javascript disabled, the form would still submit in the "proper" manner, which is why I can't understand why you would want to change the format of the query string.

Andy E
I have a stored proc ready to parse values from the url "/actionname?" This call will insert values into tables. for me using "|" can easily be parsed by my web service
Chad Sellers
A: 

Even if I do not see any reason why you would need this :), you can use the onSubmit event to create the proper url and redirect to it.

Sunny
A: 

How about you use the XMLHTMLRequest to perform your request.

You construct your URL in a string using javascript and then set-up the XHR to submit to that URL using a GET (synchronously or a synchronously, your choice).

This way you also avoid stepping on the toes of the onsubmit form event.

Alternatively, a simple workaround will be to construct your URL in a string and then set the form action attribute to this string before submitting the form with the submit() method.

dpb
+1  A: 

if you are using any java script library like jQuery, you can serialize the form which gives the data in query string format. ex: see here

Teja Kantamneni
That builds a query string like the one obtained by standard GET submit on the form. The OP wants to custom format the URL. serialize() won’t do that.
dpb