美文网首页Golang程序员Go
golang 网络编程(8)表单

golang 网络编程(8)表单

作者: zidea | 来源:发表于2019-04-25 14:45 被阅读28次
golang_real.jpg

其实本人现在有点讨厌搬砖的工作,但是有的时候还是的搬一搬。不搬那知道自己有力气。


th-14.jpeg

客户端代码
模板文件,下面是表单提交的客户端代码可能在熟悉不过了。就不多解释了。

    <form class="login_form" action="/login" method="POST">
        <div class="form_input">
            <label for="username">username</label>
            <input id="username" type="text" name="username">
        </div>
        <div class="form_input">
            <label for="password">password</label>
            <input id="password" type="password" name="password" />
        </div>
        <div class="form_submit">
            <input type="submit" value="login">
        </div>
    </form>

服务端代码

func login(w http.ResponseWriter, r *http.Request){
    fmt.Println("method: " + r.Method)
    r.ParseForm()
    if r.Method == "GET"{
        t, _ := template.ParseFiles("login.gtpl")
        t.Execute(w, nil)
    }else{
        fmt.Println("username: ", r.Form["username"])
        fmt.Println("password: ", r.Form["password"])
    }
}
  • 定义 login 的一个方法login的路由控制,当客户端发起 get 请求来访问 /login 服务端读取模板文件,返回给客户端一个登录界面就是上面的模板文件,用户完成用户名和密码填写后提交以 post 方式表单数据给服务端时候。客户端获取r.Form获取表单数据简单打印出来。这就完成一次客户端向服务端的表单提交
  • 注意要获取表单数据,客户端一定要先调用r.ParseForm()方法
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;

            background: lightgray;
        }

        .login_form {

            background: lightblue;
            padding: 12px;
        }

        .form_input {
            color: white;
            height: 24px;
            outline: none;
            border: none;
        }

        .form_submit input {
            color: dodgerblue;
            height: 24px;
            font-size: 18px;
            background: deepskyblue;
        }
    </style>
</head>

<body>
    <form class="login_form" action="/login" method="POST">
        <div class="form_input">
            <label for="username">username</label>
            <input id="username" type="text" name="username">
        </div>
        <div class="form_input">
            <label for="password">password</label>
            <input id="password" type="password" name="password" />
        </div>
        <div class="form_submit">
            <input type="submit" value="login">
        </div>
    </form>
</body>

</html>
package main

import(
    "fmt"
    "html/template"
    "log"
    "net/http"
    // "strings"
)

func index(w http.ResponseWriter, r *http.Request){

}

func login(w http.ResponseWriter, r *http.Request){
    fmt.Println("method: " + r.Method)
    r.ParseForm()
    if r.Method == "GET"{
        t, _ := template.ParseFiles("login.gtpl")
        t.Execute(w, nil)
    }else{
        fmt.Println("username: ", r.Form["username"])
        fmt.Println("password: ", r.Form["password"])
    }
}

func iconHandler(w http.ResponseWriter, r *http.Request) {
    
}


func main() {
    http.HandleFunc("/",index);
    http.HandleFunc("/login",login);

    server := &http.Server{
        Addr:":9090",
    }

    log.Println("Listening...")
    err := server.ListenAndServe()
    if err != nil{
        log.Fatal("Listen And Server ", err)
    }
}

在 web 应用开发中,我们经常会对邮件地址、用户名、电话号码以及居民身份中进行校验,下面列出了这些常用的正则表达式。

电子邮件地址
    if m, _ := regexp.MatchString(`([\w.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`,"qq123@qq.com"); !m {
        fmt.Println("invalidated email address")
    }else{
        fmt.Println("validated")
    }
手机号码
    if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`,"13840008000"); !m {
        fmt.Println("invalidated phonenumber address")
    }else{
        fmt.Println("validated phonenumber")
    }
匹配中文
    if m, _ := regexp.MatchString("^[\\x{4e00}-\\x{{9fa5}}]+$","代码"); !m {
        fmt.Println("invalidated chinese")
    }else{
        fmt.Println("validated chinese")
    }

参看 go web 编程

相关文章

  • golang 网络编程(8)表单

    其实本人现在有点讨厌搬砖的工作,但是有的时候还是的搬一搬。不搬那知道自己有力气。 客户端代码模板文件,下面是表单提...

  • Golang网络编程TCP连接

    Golang网络编程 TCP编程编写服务端package mainimport ( "bufio" "fmt"...

  • 2016规划

    es、netty(IO+网络编程)、jsf、tomcat、Nginx+lua+C redis zk golang ...

  • 001golang中的字符串编码问题无标题文章

    golang 编程中默认使用的编码是UTF-8 golang 编码库 mahonia 可以用作编码的扩展。 ht...

  • GoLang TCP网络编程

    原文链接:http://blog.csdn.net/hacker00011000/article/details/...

  • golang 网络编程实践

    webmain.go proxy.go util/config.go util/func.go util/load...

  • golang 网络编程(3)

    先定义数据结构,我们可能总是习惯用我们熟知语言例如 java 中对应的类来理解 go 语言中结构,这就错了,在学习...

  • golang 网络编程(1)

    Http 请求是一种无状态的通讯模型,无状态可能对于初学者听起来有点 confusing。这里的无状态表明每一次请...

  • Golang网络编程UDP

    UDP协议 UDP协议中文名称使用户数据报协议。UDP是一种无连接的传输层协议不需要建立连接就可以直接进行数据发送...

  • Golang网络编程实战

    1. 开张课、课程内容、说下反向代理 2. 复习课 利用协程创建两个测试web服务 3. 最简单的请求 转发 、h...

网友评论

    本文标题:golang 网络编程(8)表单

    本文链接:https://www.haomeiwen.com/subject/tdpwgqtx.html