php - How do I pass Google maps lat and lng values and save to MySQL database? -
i writing google maps application user can click want save marker filling form in infowindow , clicking submit
button saves data mysql database.
a picture of mean here:
i not know how retrieve lat
, lng
values event.latlng
, use them part of addlocation
form action.
the javascript contains addlocation
form action:
function placemarker(location) { var latlng = event.latlng; // don't know how retrieve lat & lng latlng? var contentstring = '<form action="addlocation" method="post">' + '<div id="addmarkerinfowindow">' + '<b>name:</b> <input type="text" name="name"><br>' + '<b>facilities:</b> <input type="text" name="facilities"><br>' + '<b>opening hours:</b> <input type="text" name="opening"><br>' + '<b>notes:</b> <input type="text" name="notes"><br></div>' + '<br><br><input type="submit" value="submit"> ' + '<input type="button" value="cancel" /></form>'; var infowindow = new google.maps.infowindow({ content: contentstring }); var marker = new google.maps.marker({ position: location, map: map }); infowindow.open(map,marker); }
this php code have:
(note: new php)
<?php require("db_info.php"); // opens connection mysql server $connection = mysqli_connect('localhost', $username); if (!$connection) { die('not connected : ' . mysqli_error()); } // set active mysql database $db_selected = mysqli_select_db($connection, $database); if (!$db_selected) { die('can\'t use db : ' . mysqli_error()); } ################ save location ################# if($_post) //run if there's post data { // make sure ajax request $xhr = $_server['http_x_requested_with'] == 'xmlhttprequest'; if (!$xhr) { header('http/1.1 500 error: request must come ajax!'); exit(); } // inserting data order $order = "insert markers (name, facilities, opening, lat, lng, notes) values ('$name', '$facilities', $opening, '$lat', '$lng', '$notes')"; // declare in order variable $result = mysql_query($order); //order executes if($result) { echo("<br>input data succeed"); } else { echo("<br>input data fail"); } } ?>
you can update content string use hidden fields lat
, lng
var contentstring = '<form action="addlocation" method="post">' + '<input type="hidden" name="lat" value="' + location.lat() + '" />' + '<input type="hidden" name="lng" value="' + location.lng() + '" />' + '<div id="addmarkerinfowindow">' + '<b>name:</b> <input type="text" name="name"><br>' + '<b>facilities:</b> <input type="text" name="facilities"><br>' + '<b>opening hours:</b> <input type="text" name="opening"><br>' + '<b>notes:</b> <input type="text" name="notes"><br></div>' + '<br><br><input type="submit" value="submit"> ' + '<input type="button" value="cancel" /></form>';
Comments
Post a Comment