Submitting a form and jumping to it is a common task in Golang. Submission of forms can be used for many different use cases such as user registration, searching, adding or updating data, etc. In this article, we'll explore how to write a basic form submitter and how to handle jumps.
Implement form submission
First, we need a simple HTML form to submit data. The following is a simple form containing a username and password:
<html> <head> <title>登录页面</title> </head> <body> <h1>欢迎来到我的网站</h1> <form action="/login" method="post"> 用户名: <input type="text" name="username"><br> 密码: <input type="password" name="password"><br> <input type="submit" value="提交"> </form> </body> </html>
The form uses the POST method to send submission data to the /login path. We need to set up a handler on the Go server to receive this data.
On the server side, we need to use the net/http package to process this form. We can use the http.HandleFunc() function to set a handler for form submission. Here is the code for the handler:
func loginHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() username := r.Form.Get("username") // 获取表单中的用户名 password := r.Form.Get("password") // 获取表单中的密码 // 在这里执行登录逻辑 // ... // 跳转到登录成功后的页面 http.Redirect(w, r, "/success", http.StatusSeeOther) }
The handler first parses the form data using the r.ParseForm() method. Then, we get the username and password by calling the r.Form.Get() method. Next, we can perform any login validation logic in the handler and jump to another page if needed.
Handling jump
In the above handler, we use the http.Redirect() method to jump to another page. This method receives 3 parameters: http.ResponseWriter object, *http.Request object and target URL. The parameter http.StatusSeeOther instructs us to use the 303 status code for the jump.
When the form is submitted, the server will send an HTTP response to the browser. In the response, we can set HTTP headers to tell the browser to jump to another page. This can be done by setting the "Location" HTTP header. In Go, we can use ResponseWriter objects to set HTTP headers.
In the above handler, we set the target URL to "/success" in the http.Redirect() method. After a successful login, we will jump to this URL. We need to set up a new handler in the server's handler to handle this URL.
The following is the code of the handler:
func successHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "恭喜,登录成功!") }
The handler just outputs a successful login message on the page. In real applications, we may need to perform other operations, such as adding data to the database or updating data.
Using gorilla/mux for routing
We can use the HTTP standard library to implement form submission, but its routing functionality is very limited. Therefore, we usually use third-party software packages to manage routing. In this example we will use gorilla/mux.
gorilla/mux is a very popular third-party software package that can be used to create flexible HTTP routing. Using gorilla/mux we can map multiple handlers to different routes. This makes the code easier to read and maintain.
Here is the complete code using gorilla/mux:
package main import ( "fmt" "net/http" "github.com/gorilla/mux" ) func loginHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() username := r.Form.Get("username") password := r.Form.Get("password") // 执行登录逻辑 // ... // 跳转到成功页面 http.Redirect(w, r, "/success", http.StatusSeeOther) } func successHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "恭喜,登录成功!") } func main() { r := mux.NewRouter() r.HandleFunc("/login", loginHandler).Methods("POST") r.HandleFunc("/success", successHandler) http.ListenAndServe(":8000", r) }
In this example, we use the gorilla/mux package to create a new router. We then add two handlers to the router: one to handle the form submission and another to handle the success page. We use the router.HandleFunc() method to set up the handler and specify the HTTP method. We can also use the router.PathPrefix() method to set the URL prefix of the handler.
The last line of code calls the http.ListenAndServe() function to start the server. This function receives two parameters: server address and router object.
Summary
In this article, we learned how to implement form submission in Go and handle the jump after a successful submission. We also learned about the benefits of using the gorilla/mux package to manage HTTP routing. By using these technologies, we can create beautiful and feature-rich web applications.
The above is the detailed content of golang submit form jump. For more information, please follow other related articles on the PHP Chinese website!