头图

For the end user, the form submission process is very convenient, in a way equivalent to entering data and clicking the submit button. However, from an engineering perspective, it requires an encoding mechanism to reliably send and receive this data from the client to the server for backend processing.

Use JavaScript to send form data and use PHP to receive it on the server side, then the JavaScript side needs to use encodeURIComponent() for processing.

The most common HTTP method for form submission is POST. However, for idempotent form submissions, we can also use the HTTP GET method. And, the way to specify the method is through the method attribute of the form.

For forms using the GET method, the entire form data is sent as part of the query string ( query string ). However, if we use the POST method, its data will be sent as part of the HTTP request body ( body ).

Moreover, in the latter case, we can also specify the encoding of the data through the enctype attribute of the form, which can take two values, namely application/x-www-form-urlencoded and multipart/form-data .

The default value of the enctype attribute of an HTML form is application/x-www-form-urlencoded because it handles the basic use case where the data is entirely text. However, if our use case involved backing file data, then we would have to override it with the value of multipart/form-data.

Essentially, it sends the form data as key-value pairs, separated by & characters. Additionally, the corresponding keys and values are separated by an equal sign ( = ). Also, all reserved and non-alphanumeric characters are encoded using percent encoding.

See the code below:

 // url encode your string
var string = encodeURIComponent('+'); // "%2B"
// send it to your server
window.location = 'http://example.com/?string='+string; // http://example.com/?string=%2B

注销
1k 声望1.6k 粉丝

invalid