Get the data-key from a html form

I am using a “datalist” to select in a HTML form. The downside with datalists is that you cannot store the “key” or “id” in a normal way.

http://94.237.92.101:5050

  <input list=jobs data-key="" id="job" name="job" placeholder="Search..." onchange="update(this)">
  <datalist id=jobs>
    <option data-id=11 value="11 Web"></option>
    <option data-id=42 value="42 Print"></option>
    <option data-id=43 value="43 Design"></option>
    <option data-id=44 value="44 Consulting"></option>
    <option data-id=45 value="45 Misc"></option>
  </datalist>

The user wants a “readable” description. The database need an “id”. In order to store the “id” I have managed to store the id value in a “data” format using Javascript.

var key = option.dataset.id
field.setAttribute('data-key', key);

The goal is to fetch this “data-id” or “data-key” and pass it to the database. The description that is the normal “value” of a HTML form is already in the database and does not need to be stored again. I can fetch this “data-key” using Javascript.

  var inputs = document.querySelectorAll("input[list]");
  for (var element of inputs) {
    alert(element.dataset.key) 
  }

This code does only fetch the “description”.

func submit(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	for key, value := range r.PostForm {
		fmt.Printf("%s =%s\n", key, value)
	}
}

But how do I fetch the “data-key” using Go?

Do you mean the values of the data-id attributes? Those are data attributes that are not present in the form submit. You can’t parse them from the request since the browser does not send them.

Yes. But it is stored at the top level <input “data-key”.

So I can fetch them with Javascript, but not with Golang?

You can read them from JavaScript that runs in the browser. You wouldn’t be able to read them from a nodeJS backend, similar to how you can’t read them from a go backend

You can though write JavaScript on the client, that reads those attributes on submitting and adds them to the form data.

2 Likes

Yes and No. I have to add a hidden field to store the id and then submit this as a “normal value”.

https://www.w3schools.com/tags/att_input_type_hidden.asp

A hidden form field is not a Data-Attribute anymore. It’s a form field. You should not trust its value. Though you should not trust any value that would be injected by JS in s pre submit as well.

As that can be potentially override by the user.

1 Like

Yes, but I can accept this, but the hidden field does not being parsed. How do I get values from hidden inputs?

func submit(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	for key, value := range r.PostForm {
		fmt.Printf("%s =%s\n", key, value)
	}
}

Since your code does not include the parts that actually build and submit the HTTP POST request, it is hard to tell what r.ParseForm() contains.

If there is a hidden field that have a “name” you can store the id and submit the id

 <input type="hidden" id="jobid" name="jobid" value="">

func submit(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	for key, value := range r.PostForm {
		fmt.Printf("%s =%s\n", key, value)
	}
}

jobid =[43]
codeid =[PM]

Or is there an even smarter way to do this?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.