2

I've been reading about the get method to submit form data.

So, the data from the form is appended to the action attribute's value of the form with ? as a separator.

What I fail to understand is, how I can correctly predict what the URL from a form submission will be.

I mean, lets say a form looks like this:

<form method="get" action="foobar.com/search">
  User Name : <input type="text" name="uname"></input><br/>
  Name : <input type="text" name="name"></input><br/>
  <input type="submit" value="Submit"></input>
</form>

When the submit button is clicked, what is the generated URL? It would be : foobar.com/search?[data encoded here]

But what protocol defines how it is encoded? The name might come before or after the user name.

Is it possible to write a script that would create a template URL for any form so that on replacing the corresponding %s with string data, it would create the correct URL?

THank you.

3

2 Answers 2

3

Given an application/x-www-form-urlencoded encoding (the default) and the use of GET, the process is, in brief:

For each control, in the order they appear in the form, if it is a successful control:

  1. If not the first item, add a &
  2. URL encode the name of the field
  3. Add an =
  4. URL encode the value of the field

The complete process for constructing a form data submission is described in the HTML 5 specification (specifically in The application/x-www-form-urlencoded encoding algorithm).

Is it possible to write a script that would create a template URL for any form so that on replacing the corresponding %s with string data, it would create the correct URL?

Since it isn't always possible to predict which controls will be successful until the form is submitted, no.

Sign up to request clarification or add additional context in comments.

4 Comments

What exactly does successful mean? And what if controls are not text fields?
Successful is (more of less) not disabled, clicked if it was a submit button, checked if it was a radio or checkbox. Controls that are not text fields still have values.
Ah, Thank you Quentin! For the specification too.
0

The order of parameters makes absolutely no difference, since it is an associative array.

That said, I'm fairly sure all browsers will send the variables in the order they appear in the source. So in your case it will be ?uname=[input]&name=[input]

1 Comment

It is not an associative array. It is a string. Parsers may generate an associative array from it. Given two values with the same name, the order because highly significant as some parsers will discard data based on the order in which it appears.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.