Aerovistae Aerovistae - 4 months ago 9
Ajax Question

What causes the user to go to new a page when they submit a form, and can it be done in JavaScript?

You supply the

action
and
method
properties on a form, and then when it's submitted, it completes that HTTP
method
using the url specified in
action
. The end result takes the user to a new page, the page returned by the server in response to a POST (or whatever) to that target URL.

What causes this redirect? Is it an intrinsic property of how HTML form elements work, or is it just a redirect header in the HTTP response?

I'm trying to reproduce that effect with an ajax POST, without just manually writing a line of JS to do the redirect~ I want it to happen automatically, the same way.

Is submitting a form essentially the same as going to the url
www.myHostDomain.com/mySpecifiedFormAction?myFirstFormField=myFirstFormValue&mySecondFormField=mySecondFormValue
?

That can't be right, since the browser bar is always a GET request.

Answer

Form's action attribute serves a role of navigation URL, a.k.a. "plan to navigate". target attribute defines where to load that response returned by the server.

So yes, that is "an intrinsic property of how HTML form elements work".

Algorithm of handling form submission is defined in HTML4 and HTML5 forms specifications.

Submitting the form is essentially an ordinary URL navigation (by <a href> elements) but with automatic parameters gathering that go either into final navigation URL as parameters ( <form method="get"> ) or as request body parameters ( <form method="post"> )

Comments