dourong8495 2018-03-26 10:53
浏览 27

如何将GO产生的值分配给HTML的文本字段?

I am new on GO. The following code will save and retrieve the data from database (mongodb) document name profile.

GO code

package main

import (
    "fmt"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
    "html/template"
    "log"
    "net/http"
)

type USER struct {
   FirstName string `json:"FirstName"`
   LastName  string `json:"LastName"`
   Email     string `json:"Email"`
   Mobile    string `json:"Mobile"`
}

func login(w http.ResponseWriter, r *http.Request) {
    fmt.Println("method:", r.Method)
    if r.Method == "GET" {
        t, _ := template.ParseFiles("index.html")
        t.Execute(w, nil)
    } else {
        r.ParseForm()
        //fmt.Println(r.Form)
        if r.Form["button"][0] == "Submit" {
            fmt.Println(r.Form)
            session, err := mgo.Dial("mongodb://127.0.0.1:27017/user")
            if err != nil {
                panic(err)
            }
            defer session.Close()
            session.SetMode(mgo.Monotonic, true)
            c := session.DB("user").C("profile")
            doc := USER{
                FirstName: r.Form["first"][0],
                LastName:  r.Form["last"][0],
                Email:     r.Form["email"][0],
                Mobile:    r.Form["mobile"][0],
            }
            err = c.Insert(doc)
            if err != nil {
                panic(err)
            }
        } else {
            session, err := mgo.Dial("mongodb://127.0.0.1:27017/user")
            if err != nil {
                panic(err)
            }
            defer session.Close()
            session.SetMode(mgo.Monotonic, true)
            c := session.DB("user").C("profile")
            if c!=nil{
            result := USER{}
            err = c.Find(bson.M{"email": r.Form["email"][0] }).Select(bson.M{"firstname":"firstname", "lastname":"lastname", "email":"email", "mobile":"mobile"}).One(&result)
            if err != nil {
                panic(err)
            }
            fmt.Println("FirstName", result)
            fmt.Println("FirstName", result.FirstName)
            fmt.Println("LastName", result.LastName)
            fmt.Println("Email", result.Email)
            fmt.Println("Mobile", result.Mobile)
        }
    }
}

func main() {
    http.HandleFunc("/login", login)
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <form id="form1" method="post">
        <input id= "firstname" type="text" name="FirstName"  placeholder="Enter your firstname"><br><br>
        <input id= "lastname" type="text" name="LastName" placeholder="Enter your lastname"><br><br>
        <input id= "email" type="text" name="Email" placeholder="Enter your email"><br><br>
        <input id= "mobile" type="text" name="Mobile" placeholder="Enter your mobile"><br><br>
        <button id= "button1" class="button" name="button" type="button" value="Submit">Submit</button>
        <button id= "button2" class="button" name="button" type="submit" value="Search">Search</button>
    </form>
    <script>
        $(document).ready(function(){
            $('.button').click(function(){
                var id = $(this).attr('value');
                alert(id);
            });
            $('.button').on('click', function(e){
                button=this.value;
                console.log(button);
                e.preventDefault();
                var FirstName =$("#firstname").val(),
                LastName =$("#lastname").val(),
                Email =$("#email").val(),
                Mobile =$("#mobile").val();
                console.log(FirstName);
                $.ajax({
                    url:"/login",
                    type:"POST",
                    data: {'button':button, "first":FirstName, "last":LastName, "email":Email, "mobile":Mobile},
                    success: function(results) {
                        console.log(results);
                        $('#response').html(results);
                    }
                });
            });
        });
    </script>

This is the code from which data can save through ajax request in the database document profile. when, I want to retrieve the data from the database then it retrieve the data but in terminal only I searched it for google how to assign the value of go to html tags but not got success. So tell me how to solve this problem.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度