Fix grammar, typos and sentence flow for clarity
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
# 4 User form
|
||||
|
||||
User form is the tool that is commonly used when we develop web applications, it provides ability to communicate between clients and servers. You must know form very much if you are web developers; if you are C/C++ programmers, you may want to ask: what is the user form?
|
||||
A user form is something that is very commonly used when we develop web applications. It provides the ability to communicate between clients and servers. You must be very familiar with forms if you are a web developer; if you are a C/C++ programmer, you may want to ask: what is a user form?
|
||||
|
||||
Form is the area that contains form elements. Users can input information in these elements, like text box, drop down list, radio buttons, check box, etc. We use form tag `<form>` to define form.
|
||||
A form is an area that contains form elements. Users can input information into form elements like text boxes, drop down lists, radio buttons, check boxes, etc. We use the form tag `<form>` to define forms.
|
||||
|
||||
<form>
|
||||
...
|
||||
@@ -10,11 +10,11 @@ Form is the area that contains form elements. Users can input information in the
|
||||
...
|
||||
</form>
|
||||
|
||||
Go has already had many convenient functions to deal with use form, like you can easily get form data in HTTP requests, and there are easy to integrate to your own web applications. In section 4.1, we are going to talk about how to handle form data in Go, and because you cannot believe any data from clients, you have to verify data before use them. After that, we will show some examples about verifying form data in section 4.2.
|
||||
Go already has many convenient functions to deal with user forms. You can easily get form data in HTTP requests, and they are easy to integrate into your own web applications. In section 4.1, we are going to talk about how to handle form data in Go. Also, since you cannot trust any data coming from the client side, you must first verify the data before using it. We'll go through some examples about how to verify form data in section 4.2.
|
||||
|
||||
We say that HTTP is stateless, how can we identify that these forms are from same user? And how to make sure that one form can only be submitted once? We have more detailed explanation about Cookie (Cookie is the information that can be saved in the client side, and put in the request header when the request is sent to the server) in both sections 4.3 and 4.4.
|
||||
We say that HTTP is stateless. How can we identify that certain forms are from the same user? And how do we make sure that one form can only be submitted once? We'll look at some details concerning cookies (a cookie is information that can be saved on the client side and added to the request header when the request is sent to the server) in both sections 4.3 and 4.4.
|
||||
|
||||
Another big feature of form is uploading files. In section 4.5, you will learn how to use this feature and control file size before it starts uploading in Go.
|
||||
Another big use-case of forms is uploading files. In section 4.5, you will learn how to do this as well as controlling the file upload size before it begins uploading, in Go.
|
||||
|
||||
## Links
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# 4.1 Process form inputs
|
||||
|
||||
Before we start talking, let's take a look a simple example of use form, save it as `login.gtpl` in your project folder.
|
||||
Before we begin, let's take a look at a simple example of a typical user form, saved as `login.gtpl` in your project folder.
|
||||
|
||||
<html>
|
||||
<head>
|
||||
@@ -15,9 +15,9 @@ Before we start talking, let's take a look a simple example of use form, save it
|
||||
</body>
|
||||
</html>
|
||||
|
||||
This form will submit to `/login` in server. After user clicked log in button, the data will be sent to `login` handler in server router. Then we need to know it uses POST method or GET.
|
||||
This form will submit to `/login` on the server. After the user clicks the login button, the data will be sent to the `login` handler registered by the server router. Then we need to know whether it uses the POST method or GET.
|
||||
|
||||
It's easy to know through `http` package, let's see how to handle form data in log in page.
|
||||
This is easy to find out using the `http` package. Let's see how to handle the form data on the login page.
|
||||
|
||||
package main
|
||||
|
||||
@@ -66,27 +66,27 @@ It's easy to know through `http` package, let's see how to handle form data in l
|
||||
}
|
||||
|
||||
|
||||
Now we know use `r.Method` to get request method, and it returns a string like "GET", "POST", "PUT", etc.
|
||||
Here we use `r.Method` to get the request method, and it returns an http verb -"GET", "POST", "PUT", etc.
|
||||
|
||||
In function `login`, we use `r.Method` to check if it's a log in page or log in process logic, which means you just open this page, or you are trying to log in. Serve shows page only when it uses GET method, process log in logic when it uses POST method.
|
||||
In the `login` function, we use `r.Method` to check whether it's a login page or login processing logic. In other words, we check to see whether the user is simply opening the page, or trying to log in. Serve shows the page only when the request comes in via the GET method, and it executes the login logic when the request uses the POST method.
|
||||
|
||||
You should see following interface after you opened `http://127.0.0.1:9090/login` in your browser.
|
||||
You should see the following interface after opening `http://127.0.0.1:9090/login` in your browser.
|
||||
|
||||

|
||||
|
||||
Figure 4.1 User log in interface
|
||||
Figure 4.1 User login interface
|
||||
|
||||
Server will not print anything after we typed user name and password because handler doesn't parse form until we call `r.ParseForm()`. Let's add `r.ParseForm()` before `fmt.Println("username:", r.Form["username"])`, compile and test again. You should see information is printed in server side now.
|
||||
The server will not print anything until after we type in a username and password, because the handler doesn't parse the form until we call `r.ParseForm()`. Let's add `r.ParseForm()` before `fmt.Println("username:", r.Form["username"])`, compile our program and test it again. You will find that the information is printed on the server side now.
|
||||
|
||||
`r.Form` contains all request arguments, like query-string in URL, data in POST and PUT. If the data have conflicts like have same name, it will save into a slice with multiple values. In Go documentation, it says Go will save data of GET and POST in different places.
|
||||
`r.Form` contains all of the request arguments, for instance the query-string in the URL and the data in POST and PUT. If the data has conflicts, for example parameters that have the same name, the server will save the data into a slice with multiple values. The Go documentation states that Go will save the data from GET and POST requests in different places.
|
||||
|
||||
Try to change value of action in form `http://127.0.0.1:9090/login` to `http://127.0.0.1:9090/login?username=astaxie` in file `login.gtpl`, test again, and you will see the slice is printed in server side.
|
||||
Try changing the value of the action in the form `http://127.0.0.1:9090/login` to `http://127.0.0.1:9090/login?username=astaxie` in the `login.gtpl` file, test it again, and you will see that the slice is printed on the server side.
|
||||
|
||||

|
||||
|
||||
Figure 4.2 Server prints request data
|
||||
|
||||
The type of `request.Form` is `url.Value`, it saves data with format `key=value`.
|
||||
The type of `request.Form` is `url.Value`. It saves data with the format `key=value`.
|
||||
|
||||
v := url.Values{}
|
||||
v.Set("name", "Ava")
|
||||
@@ -98,7 +98,7 @@ The type of `request.Form` is `url.Value`, it saves data with format `key=value`
|
||||
fmt.Println(v.Get("friend"))
|
||||
fmt.Println(v["friend"])
|
||||
|
||||
**Tips** Request has ability to access form data by method `FormValue()`. For example, you can change `r.Form["username"]` to `r.FormValue("username")`, and it calls `r.ParseForm` automatically. Notice that it returns the first value if there are arguments with same name, and it returns empty string if there is no such argument.
|
||||
**Tips** Requests have the ability to access form data using the `FormValue()` method. For example, you can change `r.Form["username"]` to `r.FormValue("username")`, and Go calls `r.ParseForm` automatically. Notice that it returns the first value if there are arguments with the same name, and it returns an empty string if there is no such argument.
|
||||
|
||||
## Links
|
||||
|
||||
|
||||
Reference in New Issue
Block a user