简体   繁体   中英

I'm trying to get a Javascript Function Value into a Mysql Database

I have done a bit of Php, mysql & Html programing but haven't done anything with javascript. I have been looking online but haven't found out how to do this. Below is code that gets the current users GPS and shows it on the page . I need to be able to take the GPS coordinates and insert it into my Mysql Database. I know how to insert php into a mysql database but I have no clue how to get the Javascript GPS coordinates into my database. Any help would be much appreciated.

I hope this makes sense but if you are confused please let me know?

<script language="javascript" type="text/javascript">
        function getLocation() {
            // Get location no more than 5 minutes old. 300000 ms = 5 minutes.
            navigator.geolocation.getCurrentPosition(showLocation, showError, {enableHighAccuracy:true,maximumAge:300000});
        }

        function showError(error) {
            alert(error.code + ' ' + error.message);
        }

        function showLocation(position) {
            geoinfo.innerHTML= position.coords.latitude + ',' + position.coords.longitude;
        }
    </script>
<body onload="getLocation()">
<div id="geoinfo"></div>
</body>

This is a great time to learn Ajax http://api.jquery.com/jquery.ajax/ its how you get data from the front end (javascript, html...) to your php server side where the database also resides.

Your Ajax will go in your script tag just like javascript:

var valueToInsert = "some Val";
$.ajax({
        type: "POST",
        url: "your_php_page_to_insert_into_mysql.php",
        data: {
            valueToInsert: val
        },
        success:function(data){
            alert(data); //do something when done successfully
        },
        error:function(){
            alert("error"); // do something when done unsuccessfully
        },
        statusCode: {
            404: function() {
              alert( "page not found" );
            }
        }
    });

On your php page you can use POST to get your values

<?php
        if(isset($_POST['valueToInsert']))
            $valueToInsert = $_POST['valueToInsert'];

        //use $valueToInsert in your MySql Query
?>

The script you have written will be executed once it is recieved by a browser. The browser does not have any direct connection back to any My SQL server - therefore, you will need to POST the data back to a server, and have that server write the recieved data to the MySQL database.

If you insert the jQuery scripts into your page, you can easily use their functions to send the data to a URL your server is listening on:

All you need is jQuery and ajax to pass the data from the client to the server - here's an example for the clientside code:

$.post("serverside-data-receiver.php", { 
            coordsLatitude: position.coords.latitude, 
            coordsLongitude: position.coords.longitude
});

Just put that code in your JavaScript function and create the PHP part which receives the data and does all the DB stuff.

Further reading about jQuery post: http://api.jquery.com/jquery.post/

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