how can I validate and process html form (taking in inpute from the ui login interface) using fiber framework golang programming language

huangapple go评论80阅读模式
英文:

how can I validate and process html form (taking in inpute from the ui login interface) using fiber framework golang programming language

问题

请帮我使用Fiber框架在Golang中进行输入处理。

<form action="/" method="POST" novalidate>
	<div>
		<p><label>您的电子邮件:</label></p>
		<p><input type="email" name="email"></p>
	</div>
	<div>
		<p><label>您的留言:</label></p>
		<p><textarea name="content"></textarea></p>
	</div>
	<div>
		<input type="submit" value="发送消息">
	</div>
</form> ```

<details>
<summary>英文:</summary>

please help me to take input with fiber golang framework

``` &lt;h1&gt;Contact&lt;/h1&gt;
&lt;form action=&quot;/&quot; method=&quot;POST&quot; novalidate&gt;
	&lt;div&gt;
		&lt;p&gt;&lt;label&gt;Your email:&lt;/label&gt;&lt;/p&gt;
		&lt;p&gt;&lt;input type=&quot;email&quot; name=&quot;email&quot;&gt;&lt;/p&gt;
	&lt;/div&gt;
	&lt;div&gt;
		&lt;p&gt;&lt;label&gt;Your message:&lt;/label&gt;&lt;/p&gt;
		&lt;p&gt;&lt;textarea name=&quot;content&quot;&gt;&lt;/textarea&gt;&lt;/p&gt;
	&lt;/div&gt;
	&lt;div&gt;
		&lt;input type=&quot;submit&quot; value=&quot;Send message&quot;&gt;
	&lt;/div&gt;
&lt;/form&gt; ```

</details>


# 答案1
**得分**: 1

你可以使用`c.FormValue`来处理简单的表单,需要注意的是返回值始终是一个字符串。所以如果你想发送整数,你需要在处理程序中手动将它们转换为整数。对于更复杂的表单,比如发送文件,你可以使用`c.MultipartForm`。

```go
func main() {
    // 创建一个新的fiber实例并在整个应用程序中使用
    app := fiber.New()

    // 中间件,允许所有客户端使用http进行通信并允许跨域
    app.Use(cors.New())

    // 主页
    app.Get("/", func(c *fiber.Ctx) error {
        // 使用传递的内容渲染“index”模板
        return c.Render("index", fiber.Map{})
    })

    // app.Post因为method="POST"
    // "/"因为action="/"
    app.Post("/", handleFormUpload)

    // 在端口4000上启动开发服务器
    log.Fatal(app.Listen(":4000"))
}

func handleFormUpload(c *fiber.Ctx) error {
    email := c.FormValue("email")
    content := c.FormValue("content")

    fmt.Println(email, content)
    return c.JSON(fiber.Map{"status": 201, "message": "Created!"})
}

希望对你有帮助!

英文:

You can use c.FormValue for simple forms, and keep in mind that the return value is always a string. So if you want to send integers as well you will have to manually convert them to integers in handlers. for more complex forms like sending files you can use c.MultipartForm.

func main() {
	// create new fiber instance  and use across whole app
	app := fiber.New()

	// middleware to allow all clients to communicate using http and allow cors
	app.Use(cors.New())

	// homepage
	app.Get(&quot;/&quot;, func(c *fiber.Ctx) error {
		// rendering the &quot;index&quot; template with content passing
		return c.Render(&quot;index&quot;, fiber.Map{})
	})

	// app.Post because method=&quot;POST&quot;
    // &quot;/&quot; because action=&quot;/&quot;
	app.Post(&quot;/&quot;, handleFormUpload)

	// start dev server on port 4000
	log.Fatal(app.Listen(&quot;:4000&quot;))
}

func handleFormUpload(c *fiber.Ctx) error {
	email := c.FormValue(&quot;email&quot;)
	content := c.FormValue(&quot;content&quot;)

    fmt.Println(email, content)
    return c.JSON(fiber.Map{&quot;status&quot;: 201, &quot;message&quot;: &quot;Created!&quot;})
}

huangapple
  • 本文由 发表于 2022年5月12日 07:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/72208599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定