user2250692
user2250692

Reputation: 35

HTML5 Geolocation, JavaScript, and Storing in Database

The following code is JavaScript in a JSP. I want to store the user’s latitude and longitude locations in a MySQL database by setting the value of two hidden fields on the HTML (called “latitude” and “longitude”). However, when the parameters "latitude" and "longitude" are sent to the database, they are null! Why doesn’t this code work? Any help would be greatly appreciated!

//in my onSubmit() JavaScript function 
latitude = position.coords.latitude;
longitude = position.coords.longitude;
document.getElementById("latitude").value = String(latitude);
document.getElementById("longitude").value = String(longitude);
document.getElementById("study_session_form").action = "CreateStudySessionServlet";
document.getElementById("study_session_form").submit();

//in my HTML
<td><input type="hidden" id="latitude" /></td>                   
<td><input type="hidden" id="longitude" /></td>
<td><button id="submit_button” onclick="onSubmit();">Submit</button></td>

Upvotes: 1

Views: 687

Answers (1)

Adeel
Adeel

Reputation: 19228

you need to set name attribute, ID's are not sent during form submission. E.g.

<input type="hidden" id="longitude" name="longitude" />

Upvotes: 6

Related Questions