简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM