Results 1 to 4 of 4

Thread: Global/local variables

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Global/local variables

    I think my problem here is that the var_set variable is not being passed anyone see something obviously wrong? It's only suppose to be set to 1 when the form is submitted. Thanks... This is the view source I can post with raw php if needed.


    Code:
    <body onload="load(var_set=0);">
    <form method="POST" onsubmit="pointonmap(var_set=1);">
    	<input type="text" name="address" /><input type="submit" value="Add a Point" />
    </form>
    <script type="text/javascript">
    var newpoint;
    var change_loop = 1;
    var php_lat=new Array();
    var php_lng=new Array();
    php_lat[0] = 31.046051;
    php_lng[0] = 34.851612;
    php_lat[1] = 31.046051;
    php_lng[1] = 34.851612;
        //<![CDATA[
    	function load () {
        if (GBrowserIsCompatible()) {
    	  var gmarkers = [];
    	  var i = 0;
    	  var point;
    	  var bounds = new GLatLngBounds();
    	  var marker;
    	  map = new GMap2(document.getElementById("map"));
    	  map.addControl(new GLargeMapControl());
    	  map.addControl(new GMapTypeControl());
    	  var blueIcon = new GIcon(G_DEFAULT_ICON);
    	  blueIcon.image = "images/blue.png";
    	  markerOptions2 = { icon:blueIcon };
    	  if (var_set == 1 ) { 
     change_loop = 2;
    php_lat[2] = newpoint.lat;
    php_lng[2] = newpoint.lng;
    }
    	  
    	  function createMarker() {
    		marker = new GMarker(point);
    		i++;
    		return marker;
          }
          function Process_Addresses() {
          for (var i=0; i < change_loop; i++) {
    			var lat = php_lat[i];
    			var lng = php_lng[i];
    			point = new GLatLng(lat,lng);
    			bounds.extend(point);
    			if ( i == 2) {
    			marker = new GMarker(newestpoint, markerOptions2);
    			i++;
    			} else {
    			marker = createMarker(point);
    			}
    			map.addOverlay(marker);
    			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    		  }
    		}
        Process_Addresses();
    	}    else {
          alert("Sorry, the Google Maps API is not compatible with this browser");
        }
        // This Javascript is based on code provided by the
        // Community Church Javascript Team
        // http://www.bisphamchurch.org.uk/   
        // http://econym.org.uk/gmap/
    	}
    	function pointonmap() {
    	var_set = 1;
    		var geocoder = new GClientGeocoder();
    		geocoder.getLocations(document.forms[0].address.value, function (response) {
    			if (!response || response.Status.code != 200)
    				{
    						alert("Sorry, we were unable to find that address.");
    				}
    				else
    				{
    					newpoint = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};	
    					load();
    				}
    			});
    			}
        //]]>
        </script>
    					</div>
    	<div id="map" />
    	</body>

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <body onload="load(var_set=0);">
    <form method="POST" onsubmit="var_set=1;pointonmap();">
    	<input type="text" name="address" /><input type="submit" value="Add a Point" />
    </form>
    <script type="text/javascript">
    var newpoint;
    var change_loop = 1;
    var php_lat=new Array();
    var php_lng=new Array();
    php_lat[0] = 31.046051;
    php_lng[0] = 34.851612;
    php_lat[1] = 31.046051;
    php_lng[1] = 34.851612;
        //<![CDATA[
    	function load () {
        if (GBrowserIsCompatible()) {
    	  var gmarkers = [];
    	  var i = 0;
    	  var point;
    	  var bounds = new GLatLngBounds();
    	  var marker;
    	  map = new GMap2(document.getElementById("map"));
    	  map.addControl(new GLargeMapControl());
    	  map.addControl(new GMapTypeControl());
    	  var blueIcon = new GIcon(G_DEFAULT_ICON);
    	  blueIcon.image = "images/blue.png";
    	  markerOptions2 = { icon:blueIcon };
    	  if (var_set == 1 ) {
     change_loop = 2;
    php_lat[2] = newpoint.lat;
    php_lng[2] = newpoint.lng;
    }
    
    	  function createMarker() {
    		marker = new GMarker(point);
    		i++;
    		return marker;
          }
          function Process_Addresses() {
          for (var i=0; i < change_loop; i++) {
    			var lat = php_lat[i];
    			var lng = php_lng[i];
    			point = new GLatLng(lat,lng);
    			bounds.extend(point);
    			if ( i == 2) {
    			marker = new GMarker(newestpoint, markerOptions2);
    			i++;
    			} else {
    			marker = createMarker(point);
    			}
    			map.addOverlay(marker);
    			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    		  }
    		}
        Process_Addresses();
    	}    else {
          alert("Sorry, the Google Maps API is not compatible with this browser");
        }
        // This Javascript is based on code provided by the
        // Community Church Javascript Team
        // http://www.bisphamchurch.org.uk/
        // http://econym.org.uk/gmap/
    	}
    	function pointonmap() {
        alert(var_set); // alerts 1
    		var geocoder = new GClientGeocoder();
    		geocoder.getLocations(document.forms[0].address.value, function (response) {
    			if (!response || response.Status.code != 200)
    				{
    						alert("Sorry, we were unable to find that address.");
    				}
    				else
    				{
    					newpoint = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    					load();
    				}
    			});
    			}
        //]]>
        </script>
    
    	<div id="map" /></div>
    	</body>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    bluewalrus (10-12-2009)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Thanks for your attempt but unfortunately this didn't work. The var_set was still only locally available in the pointonmap, in the load function it is still being set to zero. The alert here is 0 everytime.

    Code:
    <body onload="load(var_set=0);">
    <form method="POST" onsubmit="var_set=1;pointonmap();">
    	<input type="text" name="address" /><input type="submit" value="Add a Point" />
    </form>
    <script type="text/javascript">
    var newpoint;
    var change_loop = 1;
    var php_lat=new Array();
    var php_lng=new Array();
    php_lat[0] = 31.046051;
    php_lng[0] = 34.851612;
    php_lat[1] = 42.444589;
    php_lng[1] = -76.499268;
        //<![CDATA[
    	function load () {
        if (GBrowserIsCompatible()) {
    	  var gmarkers = [];
    	  var i = 0;
    	  var point;
    	  var bounds = new GLatLngBounds();
    	  var marker;
    	  map = new GMap2(document.getElementById("map"));
    	  map.addControl(new GLargeMapControl());
    	  map.addControl(new GMapTypeControl());
    	  var blueIcon = new GIcon(G_DEFAULT_ICON);
    	  blueIcon.image = "images/blue.png";
    	  markerOptions2 = { icon:blueIcon };	  
    alert(var_set);
    if (var_set == 1 ) { 
     change_loop = 2;
    php_lat[2] = newpoint.lat;
    alert("GOTTHERE");
    php_lng[2] = newpoint.lng;
    }
    	  
    	  function createMarker() {
    		marker = new GMarker(point);
    		i++;
    		return marker;
          }
          function Process_Addresses() {
          for (var i=0; i < change_loop; i++) {
    			var lat = php_lat[i];
    			var lng = php_lng[i];
    			point = new GLatLng(lat,lng);
    			bounds.extend(point);
    			if ( i == 2) {
    			marker = new GMarker(newestpoint, markerOptions2);
    			alert ("there");
    			i++;
    			} else {
    			marker = createMarker(point);
    			}
    			map.addOverlay(marker);
    			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    		  }
    		}
        Process_Addresses();
    	}    else {
          alert("Sorry, the Google Maps API is not compatible with this browser");
        }
        // This Javascript is based on code provided by the
        // Community Church Javascript Team
        // http://www.bisphamchurch.org.uk/   
        // http://econym.org.uk/gmap/
    	}
    	function pointonmap() {
    		var geocoder = new GClientGeocoder();
    		geocoder.getLocations(document.forms[0].address.value, function (response) {
    			if (!response || response.Status.code != 200)
    				{
    						alert("Sorry, we were unable to find that address.");
    				}
    				else
    				{
    					newpoint = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};	
    					load();
    				}
    			});
    			}
        //]]>
        </script>
    	<div id="map" />

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    When you submit a form with no action defined and that has no return value for its onsubmit event, the action defaults to either '#' or to the page itself, either way the page reloads and the body onload event fires setting your variable to 0 again.

    If I've got that right and the rest of the code is workable without page reload, you should be able to fix this with:

    Code:
    <form method="POST" onsubmit="var_set=1;pointonmap(); return false;">
    If the page must reload, you must find a way to post the value of the variable and retrieve it via PHP to the onload event of the body.
    Last edited by jscheuer1; 10-12-2009 at 11:28 AM. Reason: be more precise
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    bluewalrus (10-14-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •