Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

My Blog

从此烟雨落金城,一人撑伞两人行。

Gin处理form表单

go程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main
import (
"github.com/gin-gonic/gin"
)
func gologin(c *gin.Context){
c.HTML(200, "login.html", nil)
}
func login(c *gin.Context){
username := c.PostForm("username")
password := c.PostForm("password")
c.HTML(200, "index.html", gin.H{
"username": username,
"password": password,
})
}
func main(){
e := gin.Default()
e.Static("assets", "./assets")
e.LoadHTMLGlob("tempates/*")
e.GET("/login", gologin)
e.POST("/login", login)
e.Run()
}

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>主页</h1>
{{.name}}
{{.password}}
</body>
</html>

login.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./assets/css/login.css">
<title>login</title>
</head>

<body>
<div class="form-container">
<div class="formleft">
<img src="./assets/img/touxiang.png">
</div>
<div class="formright">
<h1>欢迎回来</h1>
<p class="py-1">请先登录</p>
<form action="/login" method="post">
<label>
<p>用户名</p>
<input type="text" name="user" class="user-input">
</label>
<label>
<p>密码</p>
<input type="password" name="password" class="pw-input">
</label>
<button class="login" type="submit">登录</button>
</form>
<a href="./register"><button class="qiehuan" type="submit">注册</button></button></a>
</div>
</div>
</body>

</html>

当有多选框时,使用c.PostFormArray("name")函数得到参数数组。

评论