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.