Call php from javascript -
i have index.php contains both js , php code. php code reads gps coordinates ios app , js reads these coordinates displayed on web app.
i stuck on figuring out how call php every few seconds can new coordinates. clueless how approach , seems recursion if need call same file purpose.
what best way "refresh" php code , read new coordinates? here code:
<?php include_once('location.php') ?> <!doctype html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <title>my geolocation</title> <style> html, body, #map-canvas { height: 100%; margin: 0px; padding: 0px } </style> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script> <script> var map; google.maps.visualrefresh = true; function initialize() { var mylatlng = new google.maps.latlng(<?php echo $current_lat ?>, <?php echo $current_long ?>); var mapoptions = { zoom: 14, center: mylatlng }; map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); var marker = new google.maps.marker({ position: mylatlng, map: map, title: 'hello world!' }); yourfunction(); } function yourfunction(){ alert ("<?php echo $current_lat ?>, <?php echo $current_long ?>"); --> here same coordinates //will add code here display new lat/long once figure out how refresh them settimeout(yourfunction, 1000); } google.maps.event.adddomlistener(window, 'load', initialize); </script> </head> <body> <div id="map-canvas"></div> </body> </html> $content = file_get_contents('php://input'); $post_data = json_decode($content , true); $lat = $post_data['lat']; $long = $post_data['long']; //connect mysql $con1 = mysql_connect("localhost", "aaaaaa", "bbbbbb", "location111"); if ($con1->connect_error) { die("connection failed: " . $conn->connect_error); } $db_selected = mysql_select_db('location111'); if (!$db_selected) { die ('can\'t use foo : ' . mysql_error()); } if (!empty($lat)) { $sql = "insert locationinfo (latitude, longitude) values ('$lat', '$long');"; mysql_query($sql) or die ('error updating database: ' . mysql_error()); } $read_query = "select * locationinfo;"; $results = mysql_query($read_query) or die ('error reading database: ' . mysql_error()); $column = array(); while($row = mysql_fetch_array($results)){ $column[] = $row; } $current_lat = $column[sizeof($column) - 1]['latitude']; $current_long = $column[sizeof($column) - 1]['longitude']; ?>
you're looking implement ajax.
what separate php code javascript, , make page outputs coordinates.
you call page javascript, , you'll result in variable.
the easiest (but not best you'll come realise) way implement using jquery: http://api.jquery.com/jquery.ajax/
$.ajax("mypageurlhere", { success: function(data) { console.log("data: " + data); }, error: function() { console.log("error loading data"); } });
Comments
Post a Comment