ztredded
ztredded

Reputation: 69

How to move html data to struct in Golang

I'm new to Golang and I am trying to move data from an action field to a struct.

My func main is:

func main () {
    http.HandleFunc("/public/", func (w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, r.URL.Path[1:]) })
    http.HandleFunc("/public/new_user/", signup) 
    http.ListenAndServe(":8080", nil)
}


// struct for recieving Signup data input from signup.html
type Signup struct {
    name  string
    email string
}

// func to receive data into struc above FROM signup.html
func signup(w http.ResponseWriter, r *http.Request) {
    var S Signup 

    S.name = r.FormValue("name")
    S.email = r.FormValue("email")

    //this is just to test the output
    fmt.Println("%v\n", S.name)
    fmt.Println("%v\n", S.email)
}

I have http.HandleFunc("public/new_user/", signup) which should render the signup function. However, I am receiving a BLANK html page when I route to public/new_user instead of receiving the struct from func signup.

The S.name = r.FormValue("name") and S.email = r.FormValue("email") lines ABOVE are getting their info from the signup page which is:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
</head>

<body>

<h1>Sign Up</h1>
<br/>

<form action="/public/new_user" method="post" id="form1">

<label>Name:</label>
<input type="string" name="name" />
</br>
</br>
<label>Email:</label>
<input type="string" name="email" />
</form>

</br>

<button type="submit" form="form1" value="Submit">Submit</button>

</body>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</html>

As you can see, this page routes to public/new_user upon submit. Can anyone please tell me why public/new_user is blank seeing as how I have public/new_user in:

http.HandleFunc("/public/new_user/", signup) 

Which should render the signup function? Thanks

Upvotes: 2

Views: 1806

Answers (2)

icza
icza

Reputation: 417462

The first problem is that you registered your handler for the pattern "/public/new_user/" but you direct your form post to /public/new_user.

You have to change your HTML <form> to use this URL:

<form action="/public/new_user/" method="post" id="form1">

The importance of this is that even though visiting the URL /public/new_user works because an automatic redirect (301 Moved permanently response) happens, but the submitted form will not be "carried" over when your browser makes the 2nd request to the new URL (ending with a slash).

After doing this you will still receiving a blank page because your signup() function does not write any response (although you will now see the submitted and printed form data on your console).

If you want to see a response, either write something or redirect to another page.

Also note that fmt.Println() does not have a format string parameter, it just prints everything you pass to it. Use fmt.Printf() if you want to use a format string. Also you can use the %+v format string to print a complete struct with field names.

For example:

func signup(w http.ResponseWriter, r *http.Request) {
    var S Signup
    S.name = r.FormValue("name")
    S.email = r.FormValue("email")

    fmt.Printf("%+v\n", S)

    fmt.Fprintf(w, "Form received, you used name: %s", S.name)
}

Upvotes: 1

ahmetlutfu
ahmetlutfu

Reputation: 325

i think you are skipping form validation. You must check client input and you should put token parameter for prevent Xsrf attack.

You can use this package for validate user's inputs.

Form validator

Upvotes: 0

Related Questions