Send javascript variables to Mysql database

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).

Upvotes: 0

Views: 1924

Answers (2)

stephenthedev
stephenthedev

Reputation: 577

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'];

Upvotes: 0

Andre
Andre

Reputation: 2469

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

Upvotes: 1

Related Questions