英文:
How to handle multiple POST requests in same handler in golang?
问题
我有两个表单在signup.html文件中,我想执行它们。
- 第一个表单重定向到
/login
,但不将数据插入数据库。 - 第二个表单既不插入数据,也不重定向到注册页面。
如果我将两个表单的action
都设置为相同的链接,那么它会将数据插入数据库。如何在一个函数中执行多个POST
请求并重定向到多个页面?
谢谢!
controllers.go
func Signup(w http.ResponseWriter, r *http.Request) error {
if r.Method == "GET" {
return SignupTmpl.Execute(w, nil)
} else if r.Method == "POST" && http.MethodPost == "Register" {
register := models.RegisterUser{
Name: r.FormValue("name"),
Email: r.FormValue("email"),
Password: r.FormValue("password"),
}
values := [3]string{register.Name, register.Email, register.Password}
database.InsertRegister(values)
return LoginTmpl.Execute(w, nil)
} else if r.Method == "POST" && http.MethodPost == "Newsletter" {
Newsletter(w, r)
return SignupTmpl.Execute(w, nil)
}
return nil
}
signup.html
// 注册表单
<form class="form" method="post" action="/login">
...
<input type="submit" value="Register">
</form>
// 新闻订阅表单
<form class="newsletter" method="post" action="/signup">
...
<input type="submit" value="Newsletter">
</form>
英文:
I have two forms in the signup.html file that I want to execute.
- The first form redirects to
/login
but does not insert the data into the database. - The second form neither inserts the data nor does it redirect to the Signup page.
If I set both action
equal to the same link then it inserts the data into the database. How to execute multiple POST
requests and redirects to multiple pages in a
single function?
Thank you!
controllers.go
func Signup(w http.ResponseWriter, r *http.Request) error {
if r.Method == "GET" {
return SignupTmpl.Execute(w, nil)
} else if r.Method == "POST" && http.MethodPost == "Register" {
register := models.RegisterUser{
Name: r.FormValue("name"),
Email: r.FormValue("email"),
Password: r.FormValue("password"),
}
values := [3]string{register.Name, register.Email, register.Password}
database.InsertRegister(values)
return LoginTmpl.Execute(w, nil)
} else if r.Method == "POST" && http.MethodPost == "Newsletter" {
Newsletter(w, r)
return SignupTmpl.Execute(w, nil)
}
return nil
}
signup.html
// Signup form
<form class="form" method="post" action="/login">
...
<input type="submit" value="Register">
</form>
// Newsletter form
<form class="newsletter" method="post" action="/signup">
...
<input type="submit" value="Newsletter">
</form>
答案1
得分: 1
> ```
> http.MethodPost == "Register"
> ...
> http.MethodPost == "Newsletter"
> ```
这些比较没有意义。[`http.MethodPost` 是一个常量][1],它包含了 HTTP POST 请求的动词("POST")。常量变量比硬编码的值更受欢迎,因为与硬编码的字符串不同,拼写错误会导致编译时错误。
>```
><input type="submit" value="Newsletter">
>```
如果你想要访问这个输入标签的值,你需要给它一个 `name`:
> <input type="submit" name="operation" value="Newsletter">
然后你可以检查表单的值:
if r.Method == "GET" {
return SignupTmpl.Execute(w, nil)
} else if r.Method == "POST" {
if r.FormValue("operation") == "Register" {
...
} else if r.FormValue("operation") == "Newsletter" {
...
}
}
我个人会为所有这些操作编写不同的处理程序,然后将它们指向不同的 URL。大部分处理程序逻辑都是路由,可以通过不同的处理程序免费完成。但你现在的做法也可以 - 只需确保:
1) 给需要后续引用的输入字段命名
2) 不要将 'POST' 与 'POST' 进行比较,而是将命名的输入字段值与预期值进行比较
[1]: https://pkg.go.dev/net/http#pkg-constants
英文:
>
> http.MethodPost == "Register"
> ...
> http.MethodPost == "Newsletter"
>
These comparisons make no sense. http.MethodPost
is a constant which contains the verb for HTTP Post requests ("POST"). Constant variables are preferred to hard coded values because unlike a hard coded string, a typeo will cause a compile time error.
>
><input type="submit" value="Newsletter">
>
If you want to be able to access the value of this input tag, you need to give it a name
:
> <input type="submit" name="operation" value="Newsletter">
Then you can check that form value:
if r.Method == "GET" {
return SignupTmpl.Execute(w, nil)
} else if r.Method == "POST" {
if r.FormValue("operation") == "Register" {
...
} else if r.FormValue("operation") == "Newsletter" {
...
}
}
I personally would write a different handler for all these operations and then point them at a different URL. Most of your handler logic is routing, and that can be done for free with different handlers. But what you're doing is fine - just make sure :
- name input fields you need to refer to later
- don't compare 'POST' to 'POST', compare the named input field value to the expected value
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论