golang 网络编程(8)表单

作者 : 开心源码 本文共2926个字,预计阅读时间需要8分钟 发布时间: 2022-05-12 共199人阅读

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 mainimport(    "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 编程

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » golang 网络编程(8)表单

发表回复