I have a form that I am using that gets a signature and I wanted it to also grab the user's location in the background as an extra layer of assurance the signature is genuine (work is done on-site).
I am not very familiar with Javascript, and have been trying to get this working. I know the issue is (probably) with the scope of the variable and the parent function isn't getting the correct data passed. I even assigned static values to lat and long for testing and still the values are blank when I pass them to the parent function (send).
I've tried a number of things, such as trying to declare the variables as global, un-nesting the function and making it it's own; but I think my own lack of understanding of Javascript is preventing me from troubleshooting this effectively. If anyone could point out my error (or point me in the right direction) I'd really appreciate it, I've been pulling my hair out over this and I know it's fundamentally simple.
send : function(){
var lat = "";
var long = "";
//if (empty == false){
navigator.geolocation.getCurrentPosition(function(position) {
var coords = position.coords;
lat = coords.latitude;
long = coords.longitude;
lat = '123';
long = '1234';
//return lat;
//return long;
});
//var lat = position.coords.latitude;
//var long = position.coords.longitude;
var canvas = document.getElementById("newSignature");
var dataURL = canvas.toDataURL("image/png");
document.getElementById("saveSignature").src = dataURL;
//var replyemail = document.getElementById('replyemail').value;
var workorder = document.getElementById('workorder').value;
var printedname = document.getElementById('printedname').value;
var managertype = document.getElementById('managertype').value;
var dataform = document.createElement("form");
document.body.appendChild(dataform);
dataform.setAttribute("action","signsend/upload_file.php");
dataform.setAttribute("enctype","multipart/form-data");
dataform.setAttribute("method","POST");
dataform.setAttribute("target","_self");
dataform.innerHTML = '<input type="text" name="image" value="' + dataURL + '"/>' + '<input type="text" name="lat" value="' + lat + '"/>' + '<input type="text" name="long" value="' + long + '"/>' + '<input type="text" name="printedname" value="' + printedname + '"/>' + '<input type="text" name="managertype" value="' + managertype + '"/>' + '<input type="text" name="workorder" value="' + workorder + '"/>' + '"/>'+'<input type="submit" value="Click me" />';
dataform.submit();
}