AAH-Shoot
AAH-Shoot

Reputation: 643

URL - Encoding (JavaScript) method="GET"

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

Upvotes: 1

Views: 370

Answers (4)

user159088
user159088

Reputation:

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.

Upvotes: 0

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

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

Upvotes: 1

Sunny Milenov
Sunny Milenov

Reputation: 22310

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.

Upvotes: 0

Andy E
Andy E

Reputation: 344547

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.

Upvotes: 1

Related Questions