PDA

View Full Version : Global/local variables



bluewalrus
10-10-2009, 01:13 PM
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.



<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>

vwphillips
10-10-2009, 02:04 PM
<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>

bluewalrus
10-12-2009, 01:42 AM
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.


<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" />

jscheuer1
10-12-2009, 11:19 AM
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:


<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.