Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 1.92 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Sending data to MySQL with Ajax and PHP
  2. function getCoordPosition(){
  3.  
  4.         if(navigator.geolocation){
  5.         navigator.geolocation.getCurrentPosition(function(position){
  6.         var latitude = position.coords.latitude;
  7.         var longitude = position.coords.longitude;
  8.         $.ajaxSetup({
  9.            url: "insert-in-bdd.php",
  10.            type: "POST",
  11.         });
  12.  
  13.         $.ajax({
  14.             data: 'latitude='+latitude+'&longitude='+longitude,      
  15.             success: function (msg) {
  16.             alert (msg);},
  17.             error: function (XMLHttpRequest, textStatus, errorThrown)
  18.             {  
  19.             alert('Error submitting request.');
  20.             }
  21.  
  22.             });
  23.  
  24.     });
  25. }
  26.  
  27. }
  28.        
  29. <?php
  30. header('Content-type: text/html; charset=ISO-8859-1');
  31. try
  32. {
  33. if(isset($_POST['latitude']) && isset($_POST['longitude'])){
  34. $latitude = ($_POST['latitude']);
  35. $longitude = ($_POST['longitude']);
  36. $db = mysql_connect(localhost, root, "");
  37. $select = mysql_select_db(madb, $db);
  38. mysql_query('INSERT INTO location (lat,lng)
  39.            VALUES (:longitude, :longitude)');
  40. }}
  41.  catch(Exception $e)
  42. {
  43.     echo 'Erreur : '.$e->getMessage().'<br />';
  44.     echo 'N° : '.$e->getCode();
  45. }
  46. ?>
  47.        
  48. <?php
  49.  header('Content-type: text/html; charset=ISO-8859-1');
  50. try
  51. {
  52. if(isset($_POST['latitude']) && isset($_POST['longitude'])){
  53. $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
  54.  $bdd = new PDO('mysql:host=localhost;dbname=madb', 'root', '');
  55. $req = $bdd->prepare('INSERT INTO location(lat, lng) VALUES(:lat, :lng)');
  56. //$req = $bdd->prepare('UPDATE location SET lat = :lat');
  57. $req->execute(array(
  58. 'lat' => $_POST['latitude'],
  59. 'lng' => $_POST['longitude']
  60. ));
  61. }}
  62. catch (Exception $e)
  63. {
  64.     die('Erreur : ' . $e->getMessage());
  65. }
  66. ?>
  67.        
  68. $query="insert into location (lat,long) values('".$_POST['latitude']."','".$_POST['longitude']."');";
  69. mysql_query($query,$connection);
  70.        
  71. "INSERT INTO location (lat,lng)  VALUES ('" . $latitude . "', '" . $longitude . "')"