Jump to content

Using info from database in javascript


deefer

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.