deefer Posted May 5, 2012 Share Posted May 5, 2012 Hi, I am a total novice to all things here, so please bear that in mind when answering. Thanks. I am tring to include google maps on a site I have. But I want to pull the address from a MySQL database. Currently I have var gpsLatlng; var map; var directionDisplay; var directionsService;function initialize() { var myLatlng = new google.maps.LatLng(51.9605055, 1.3508106); var myOptions = { zoom: 14, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var WPFPMarkerMessage = "3 wolsey court felixstowe suffolk"; var MarkerIconUrl = "http://chart.apis.google.com/chart?chst=d_map_pin_icon_withshadow&chld=home|FFFF00"; var initialMarkerImage = new google.maps.MarkerImage(MarkerIconUrl); var initialmarker = new google.maps.Marker( { position: myLatlng, draggable: false, icon: initialMarkerImage, title: WPFPMarkerMessage , map: map }); directionsService = new google.maps.DirectionsService(); directionsDisplay = new google.maps.DirectionsRenderer(); directionsDisplay.setMap(map); directionsDisplay.setPanel(document.getElementById("map_directions_panel"));} function getDirections() { var FromLocation = document.getElementById("EZMBFromLocation").value; var ToLocation = "ADDRESS GOES HERE"; var request = { origin:FromLocation, destination:ToLocation, travelMode: google.maps.DirectionsTravelMode.DRIVING }; directionsService.route(request, function(result, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(result); } }); return false;} function getGpsDirections() { get_geolocation();return false;} function get_geolocation(){ if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(onSuccess, onError);}} function onError(error) {alert("GPS location not available");} var onSuccess = function(position) {gpsLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var ToLocation = "ADDRESS GOES HERE"; var request = { origin:gpsLatlng, destination:ToLocation, travelMode: google.maps.DirectionsTravelMode.DRIVING }; directionsService.route(request, function(result, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(result); } }); return false;}; function loadScript() { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize"; document.body.appendChild(script); } window.onload = loadScript; How would I go about replacing "ADDRESS GOES HERE" with an actual address stored in a MySQL dataabase? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/262112-using-info-from-database-in-javascript/ Share on other sites More sharing options...
trq Posted May 5, 2012 Share Posted May 5, 2012 Depends how your doing things. You could simply echo it with php when you create the page, or use Ajax to populate it dynamically. Quote Link to comment https://forums.phpfreaks.com/topic/262112-using-info-from-database-in-javascript/#findComment-1343264 Share on other sites More sharing options...
deefer Posted May 5, 2012 Author Share Posted May 5, 2012 Depends how your doing things. You could simply echo it with php when you create the page, or use Ajax to populate it dynamically. Hi, Thanks for the reply. Which way would be best? I will have my javascript.js as a seperate file, and I will need to have 3-4 variables where the info is called from a database. Can you recommend any tutorials for absolute beginners on how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/262112-using-info-from-database-in-javascript/#findComment-1343268 Share on other sites More sharing options...
trq Posted May 5, 2012 Share Posted May 5, 2012 It's no different to outputting html, and sorry, I haven't read a php tutorial in about 7 years. Quote Link to comment https://forums.phpfreaks.com/topic/262112-using-info-from-database-in-javascript/#findComment-1343289 Share on other sites More sharing options...
DavidAM Posted May 5, 2012 Share Posted May 5, 2012 Depends how your doing things ... Which way would be best? Depends on how you are doing things ... break ... If your page has a single location and you want to include the map, you can set the JS variables when PHP outputs the page. Just set them before your JS LINK if it refers to them directly. If your page will need to change the address based on the user's interaction, you probably want to use AJAX to run a PHP script to query the database and return the address. Quote Link to comment https://forums.phpfreaks.com/topic/262112-using-info-from-database-in-javascript/#findComment-1343305 Share on other sites More sharing options...
deefer Posted May 5, 2012 Author Share Posted May 5, 2012 Depends on how you are doing things ... break ... If your page has a single location and you want to include the map, you can set the JS variables when PHP outputs the page. Just set them before your JS LINK if it refers to them directly. If your page will need to change the address based on the user's interaction, you probably want to use AJAX to run a PHP script to query the database and return the address. It will be a single location. Just so I understand this right, are you saying that I should use PHP to pull the data from the database as strings, and then insert the strings into the javascript code? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/262112-using-info-from-database-in-javascript/#findComment-1343330 Share on other sites More sharing options...
DavidAM Posted May 5, 2012 Share Posted May 5, 2012 Since you said the main JavaScript will be in a separate file, you can have PHP output JS variables in your page and then have your LINKed in JS file reference them. So, the output HTML would look something like this: <SCRIPT> var addrStreet = "123 Main Street"; var addrCity = "Hometown"; var addrState = "TX"; var addrZip = "98765"; </SCRIPT> <SCRIPT src="/js/mapper.js" type="text/javascript"></SCRIPT> Quote Link to comment https://forums.phpfreaks.com/topic/262112-using-info-from-database-in-javascript/#findComment-1343347 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.