- Sending data to MySQL with Ajax and PHP
- function getCoordPosition(){
- if(navigator.geolocation){
- navigator.geolocation.getCurrentPosition(function(position){
- var latitude = position.coords.latitude;
- var longitude = position.coords.longitude;
- $.ajaxSetup({
- url: "insert-in-bdd.php",
- type: "POST",
- });
- $.ajax({
- data: 'latitude='+latitude+'&longitude='+longitude,
- success: function (msg) {
- alert (msg);},
- error: function (XMLHttpRequest, textStatus, errorThrown)
- {
- alert('Error submitting request.');
- }
- });
- });
- }
- }
- <?php
- header('Content-type: text/html; charset=ISO-8859-1');
- try
- {
- if(isset($_POST['latitude']) && isset($_POST['longitude'])){
- $latitude = ($_POST['latitude']);
- $longitude = ($_POST['longitude']);
- $db = mysql_connect(localhost, root, "");
- $select = mysql_select_db(madb, $db);
- mysql_query('INSERT INTO location (lat,lng)
- VALUES (:longitude, :longitude)');
- }}
- catch(Exception $e)
- {
- echo 'Erreur : '.$e->getMessage().'<br />';
- echo 'N° : '.$e->getCode();
- }
- ?>
- <?php
- header('Content-type: text/html; charset=ISO-8859-1');
- try
- {
- if(isset($_POST['latitude']) && isset($_POST['longitude'])){
- $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
- $bdd = new PDO('mysql:host=localhost;dbname=madb', 'root', '');
- $req = $bdd->prepare('INSERT INTO location(lat, lng) VALUES(:lat, :lng)');
- //$req = $bdd->prepare('UPDATE location SET lat = :lat');
- $req->execute(array(
- 'lat' => $_POST['latitude'],
- 'lng' => $_POST['longitude']
- ));
- }}
- catch (Exception $e)
- {
- die('Erreur : ' . $e->getMessage());
- }
- ?>
- $query="insert into location (lat,long) values('".$_POST['latitude']."','".$_POST['longitude']."');";
- mysql_query($query,$connection);
- "INSERT INTO location (lat,lng) VALUES ('" . $latitude . "', '" . $longitude . "')"