0

I am trying to create a basic form where the user can enter his name, a comment and click on a submit button. When user clicks on submit, his name, comment as well as geolocation (longitude + latitude) are stored in a database. I have only one problem: How can I store the javascript variable of his geolocation into the database?

I have two php files. Iamhere.php (gets the data) and Iamhere_post.php (insert data into database).

2 Answers 2

1

Populate hidden fields in your form that hold the values you need.

Create fields longitude + latitude then populate them with the geo values using javascript so they are submitted with the form

Sign up to request clarification or add additional context in comments.

Comments

0

Populate a hidden field in your HTML form for each the latitude and the longitude:

<input type='hidden' name='latitude' />
<input type='hidden' name='longitude' />

Store your geolocation to a variable and add that variable to the hidden field:

var latitude; //store the latitude to this variable.
var longitude; //store the longitude to this variable.

document.getElementsByName("latitude")[0].setAttribute("value",latitude)
document.getElementsByName("longitude")[0].setAttribute("value",longitude)

Call it with your PHP script on your database insert like normal:

$_POST['latitude'];
$_POST['longitude'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.