Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. AJAX-ul:
  2.  
  3.  
  4. <?php
  5. $servername = "sql203.byethost31.com";
  6. $username = "xxx";
  7. $password = "xxx";
  8. $dbname = "xxx";
  9.  
  10. // Creaza conexiunea
  11. $conn = new mysqli($servername, $username, $password, $dbname);
  12. // Verifica conexiunea
  13. if ($conn->connect_error) {
  14. die("Conexiunea a esuat: " . $conn->connect_error);
  15. }
  16.  
  17. $latitude = mysqli_real_escape_string($_GET['latitude']);
  18. $longitude = mysqli_real_escape_string($_GET['longitude']);
  19. echo $latitude;
  20. echo $longitude; //Verificare variabile trimise prin AJAX
  21.  
  22. mysqli_query($conn, "UPDATE coord SET Latitude=$latitude" ) or die mysqli_error(); //Actualizare valori coordonate in db
  23. mysqli_query($conn, "UPDATE coord SET Longitude=$longitude" ) or die mysqli_error(); //Actualizare valori coordonate in db
  24. ?>
  25.  
  26.  
  27. JavaScript-ul:
  28.  
  29. <p>Click the button to get your coordinates.</p>
  30.  
  31. <button onclick="getLocation()" class="btn btn-default">Try It</button>
  32.  
  33. <p id="demo"></p>
  34.  
  35. <script>
  36. var x = document.getElementById("demo");
  37.  
  38. function getLocation() {
  39. if (navigator.geolocation) {
  40. navigator.geolocation.getCurrentPosition(showPosition);
  41. } else {
  42. x.innerHTML = "Asigurati-va ca aveti GPS-ul pornit!";
  43. navigator.geolocation.getCurrentPosition(showPosition);
  44. }
  45. }
  46.  
  47. function showPosition(position) {
  48. x.innerHTML = "Latitudine: " + position.coords.latitude +
  49. "<br>Longitudine: " + position.coords.longitude; //Verificare variabile
  50. }
  51. var latitude = "test"
  52. var longitude = "test"
  53. if (window.XMLHttpRequest) {
  54. xmlhttp = new XMLHttpRequest();
  55.  
  56.  
  57. xmlhttp.open("GET", "ajax.php?latitude=" + latitude + "&longitude=" + longitude , true);
  58. xmlhttp.send();
  59.  
  60. return false;
  61. }
  62. //Transfer valori variabile din JS in PHP
  63. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement