Javascript variable define problem -- maybe scope issue?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

Javascript variable define problem -- maybe scope issue?

Post by hydroxide »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm confused as to how to get an argument from one function to another... or how to solve my issue here. When I run this, it throws a js error "marker is undefined". Anyone have any thoughts as to how I could get this loop to work?I'm trying to pass the info into the text "bubbles" that appear when you click on the marker.

[syntax="javascript"]
var map = null;
    var geocoder = null;
	<?php
		$key = 0;
		foreach($results as $key => $contact) {
			$results[$key][javavarname] = "address$key";
			$varString = "var address$key = \"$contact[address1], $contact[city], $contact[state], $contact[zip]\";\n";
			echo $varString;
			$key++;
		}
	?>

	function markAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            // alert(address + " not found");
            } else {
              map.setCenter(point, 7);
              var marker = new GMarker(point);
              map.addOverlay(marker);
            }
          }
        );
      }
    }
    
    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        geocoder = new GClientGeocoder();
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        
<?
		foreach($results as $contact) {
			echo "markAddress(" . $contact[javavarname] . ");\n";
			echo "GEvent.addListener(marker, \"click\", function() {\n";
			echo "marker.openInfoWindowHtml(" . $contact[javavarname] . ");\n";
			echo "});\n";
		}
?>
      }
    }

Weirdan | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by hydroxide on Fri Jul 21, 2006 9:01 am, edited 2 times in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It could be in the way that PHP is being used. There are some things you could clean up a bit.

Code: Select all

$varString = "var address$key = \"$contact[address1], $contact[city], $contact[state], $contact[zip]\";\n";
Maybe do this...

Code: Select all

$varString = 'var address' . $key . ' = "' . $contact[address1].$contact[city].$contact[state].$contact[zip] . '";'; 
$varString .= "\n";
And when using arrays, make sure to enclose the index in quotes...

Code: Select all

echo "markAddress(" . $contact[javavarname] . ");\n";
echo "GEvent.addListener(marker, \"click\", function() {\n";
echo "marker.openInfoWindowHtml(" . $contact[javavarname] . ");\n";
might be better off as..

Code: Select all

echo "markAddress(" . $contact['javavarname'] . ");\n";
echo "GEvent.addListener(marker, \"click\", function() {\n";
echo "marker.openInfoWindowHtml(" . $contact['javavarname'] . ");\n";
These are just possibilities, no guarantees.
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

Post by hydroxide »

I'm not sure if the way the PHP is set up is the problem. I'm getting the proper output from my php file. You can see for yourself here http://myels.com/testing/jimmy/workdash.php. I'm out of ideas :(
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

Post by hydroxide »

Well, now I changed it to use global variables, but it throws an error "'__e_' is null or not an object. This is really weird. You can see for yourself http://myels.com/testing/jimmy/workdash.php

Code: Select all

var map = null;
    var geocoder = null;
    var marker = null;
    var point = null;
	<?php
		$key = 0;
		foreach($results as $key => $contact) {
			$results[$key][javavarname] = "address$key";
			$varString = "var address$key = \"$contact[address1], $contact[city], $contact[state], $contact[zip]\";\n";
			echo $varString;
			$key++;
		}
	?>

    
    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        geocoder = new GClientGeocoder();
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
<?
		foreach($results as $contact) {
			echo "markAddress(" . $contact[javavarname] . ");\n";
			echo "GEvent.addListener(marker, \"click\", function() {\n";
			echo "marker.openInfoWindowHtml(" . $contact[javavarname] . ");\n";
			echo "});\n";
		}
?>
      }
    }
	function markAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            // alert(address + " not found");
            } else {
              map.setCenter(point, 7);
			  marker = new GMarker(point);
              map.addOverlay(marker);
            }
          }
        );
      }
    }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Javascript :arrow: Client Side.
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

Post by hydroxide »

Sorry about that
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

Post by hydroxide »

*sigh* fixed this finally: JS 0 | hydroxide 1

Code: Select all

var map = null;
    var geocoder = null;
	<?php
		$key = 0;
		foreach($results as $key => $contact) {
			$results[$key][javavarname] = "address$key";
			$results[$key][javavarname2] = "address$key" . "_phone";
			
			$varString = "var address$key = \"$contact[address1], $contact[city], $contact[state], $contact[zip]\";\n";
			echo $varString;

			$varString2 = "var address$key" . "_phone = \"<b>$contact[cliname]</b><br>$contact[address1]<br>$contact[zip] $contact[city], $contact[state]<br>$contact[phone]\";\n";
			echo $varString2;
			$key++;
		}
	?>

    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        geocoder = new GClientGeocoder();
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
<?
		foreach($results as $contact) {
			echo "markAddress(" . $contact[javavarname] . ", " . $contact[javavarname2] . ");\n";
		}
?>
      }
    }

    function markAddress(address, info) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            // alert(address + " not found");
            } else {
              map.setCenter(point, 7);
              var marker = new GMarker(point);
              map.addOverlay(marker);
<?
			echo "GEvent.addListener(marker, \"click\", function() {\n";
			echo "marker.openInfoWindowHtml(info);\n";
			echo "});\n";			

?>			  
            }
          }
        );
      }
    }
Post Reply