If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Assigning a js variable to a php variable

Hi,
I need to assign a JS variable to a PHP variable. Even though I can do it in a simple program, I can’t make it work in a slightly imbricated one.

In this program, I am in a JS loop, where I call a « google map API ». In this loop, for each flag displayed on the map I will attach a marker. The contents of the marker is coming from a PHP function (meteorlogical information). Cannot do otherwise beacause I am using « file_get_contents » and it is very difficult to do that in JS (HttpRequest for example won’t work for this specific URL).

The contents returned by the PHP function is therefore indexed to the JS loop index. Since these are parallel, I also used a "$i=0;" then "$i++;" but this will execute only once. I get "$i=1", but then it stays at this value. So I get the same marker on every flag !!

Basically I need « $i=j » or « $i++ » to work. As I said, the following simple code works fine :

Here's the initial scenario: From the server side I read a file containing information on the 100 meteorological stations (geographical coordinates). This is PHP.

Then to JS. Easily the information is put into JS variables and with var map= new google.maps.Map .... I display 100 flags on the map with the names of the stations. So this is page load. A very nice map, and you can even get the phone number of the station by putting your mouse on it. Everything is good.

So now the 2nd part. For all of these stations I get meteorological information which I display with "var marker=new google.Maps.marker. And if the user clicks on the flag he gets the information. So as I said works for the first one.

These two mecanisms I've already made work separately. You can see them in my blog. Here you have the map "http://www.volets10.fr/V10stationsmeteo.php" and here you have the meteo (for Roissy CDG) "http://www.volets10.fr/V10metarETtafV5.php"

OK thanks, let me work on that. In the meanwhile I did get the result which I wanted (for 10 stations not 100) by typing out "manually" one by one the call to the php function !! Naturally, the sequence of the station list MUST NOT CHANGE. Or, people will not understand the weather over their heads :-)

Hi,
Kindly make this work for two stations one after the other..like :
var stt='LFPG';
sndRqt('V10ajx_mtr.php?arp='+stt,function(r){
alert(r.responseText)
});

var stt='LFOP';
sndRqt('V10ajx_mtr.php?arp='+stt,function(r){
alert(r.responseText)
});
I think we have a synhcronisation problem. The Httprequest must be synchronuous I think? Otherwise I get the second content and not the first one !!

but is it really so necessary to get up to the second information that you make an ajax call with every click on a marker? how long do you think people will be looking at your page for that the information will become outdated between the time they click on the markers?

Looking back on your original post, I think you were having a problem with function closure. You can have a look at one solution, below, calling createMarker function from within the loop (and using the yql to get the data, because I don't know php ):

makes one globally accessible infowindow and places a listener on each marker so that the one window will move and change contents on the click. If you make the markers in the loop like you are doing you don't get closure.