从一个模板中处理多个表单

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

Processing multiple forms from an only template

问题

我有两个表单放在一个模板中,我该如何识别每个 HTML 表单,以便在处理程序中进行处理呢?
在 POST 处理程序代码中,是否可以获取表单名称?

我正在使用 nosurf,因此我必须在同一个请求中生成和检查令牌,也许我做错了...

<form action="/form" method="post" name="form1">
    <label class="control-label">Set A</label>
    <div class="controls">
        <input type="text" id="my" name="my">
    </div>
    <div style="display:none;">
        <input name="_formkey" type="hidden" value="{{.token}}">
    </div>	    	
</form>

<form action="/form" method="post" name="form2">
    <label class="control-label">Set thing</label>
    <div class="controls">
        <input type="text" id="thing" name="thing">
    </div>
    <div style="display:none;">
        <input name="_formkey" type="hidden" value="{{.token}}">
    </div>	    	
</form>

我的处理程序:

func myHandler(w http.ResponseWriter, r *http.Request) {
    switch r.Method{
    case "GET":
        data := map[string]interface{}{
            "key": nosurf.Token(req),
        }
        if err := renderTemplate(w, "base", data); err != nil {
            log.Error(err)
        }

    case "POST":
        // 如何获取表单值?
        if r.FormValue("my"){}
        ...
        if r.FormValue("thing"){}
        ...

    }
}

谢谢!

英文:

I have two forms into a template, how can I identify every html form in order
to process it into my handler?<br>
Is possible get the form name in the post handler code?<br><br>
I'm using nosurf, therefore I must generate and check the
token in the same request, maybe I'm doing wrong..

    &lt;form action=&quot;/form&quot; method=&quot;post&quot; name=&quot;form1&quot;&gt;
		 &lt;label class=&quot;control-label&quot;&gt;Set A&lt;/label&gt;
			&lt;div class=&quot;controls&quot;&gt;
  				&lt;input type=&quot;text&quot; id=&quot;my&quot; name=&quot;my&quot;&gt;
			&lt;/div&gt;
      &lt;div style=&quot;display:none;&quot;&gt;
      &lt;input name=&quot;_formkey&quot; type=&quot;hidden&quot; value=&quot;{{.token}}&quot;&gt;
      &lt;/div&gt;	    	
    &lt;/form&gt;

    &lt;form action=&quot;/form&quot; method=&quot;post&quot; name=&quot;form2&quot;&gt;
		 &lt;label class=&quot;control-label&quot;&gt;Set thing&lt;/label&gt;
			&lt;div class=&quot;controls&quot;&gt;
  				&lt;input type=&quot;text&quot; id=&quot;thing&quot; name=&quot;thing&quot;&gt;
			&lt;/div&gt;
      &lt;div style=&quot;display:none;&quot;&gt;
      &lt;input name=&quot;_formkey&quot; type=&quot;hidden&quot; value=&quot;{{.token}}&quot;&gt;
      &lt;/div&gt;	    	
    &lt;/form&gt;

My Handler

func myHandler(w http.ResponseWriter, r *http.Request) {
	switch r.Method{
	case &quot;GET&quot;:
		data:=map[string]interface{}{
			&quot;key&quot;:nosurf.Token(req),
		}
		if err := renderTemplate(w, &quot;base&quot;, data); err != nil {
    		log.Error(err)
    	}

	case &quot;POST&quot;:
        // how?
		if r.FormValue(&quot;my&quot;){}
        ...
		if r.FormValue(&quot;thing&quot;){}
        ...

	}

}

Thanks

答案1

得分: 1

在处理程序代码中获取表单名称是可能的吗?

我认为这是不可能的,但是你可以在另一个隐藏的输入字段中发送表单名称。

英文:

> Is possible get the form name in the post handler code?

I don't think that's possible, but you can send the form name in another hidden input field.

huangapple
  • 本文由 发表于 2014年11月8日 23:10:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/26818516.html
匿名

发表评论

匿名网友

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

确定