Jump to content

Refresh a javascript function containg PHP data.


UG1234

Recommended Posts

Hi,  i am building a mapping application that plots a  markers on a map from a csv file. The coordinates of the csv file can change whist the user is viewing the map so i want the function that draws the marker to refresh every 60 seconds.

 

My first attempt at the ajax refresh hasnt worked. I have pasted in the code from the w3schools tuturial. I have then put the function thats draws the marker in a seperate file called draw.php and attempted to call the it.

 

When the ajax works i plan to use a setinterval() command on the Loadphpdoc function.

 

Thanks for any help in advance

<?PHP

 

$file_to_use = "test2.csv";

 

?>

 

<script type="text/javascript" src="http://localhost:8080/OSGBWebMapTools/openspace.js">

</script>

<script type="text/javascript">

 

var osMap;

 

function init() {

 

osMap = new OpenSpace.Map('map');

 

osMap.setCenter(new OpenSpace.MapPoint(438760, 114760), 7);

 

}

 

var url = "draw.php"

 

 

function loadphpDoc(url)

{

if (window.XMLHttpRequest)

{// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();

}

else

{// code for IE6, IE5

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.open("GET",url,false);

xmlhttp.send(null);

document.getElementById('test').innerHTML=xmlhttp.responseText;

}

 

</head>

// <body onload="init()"> *

<div id="map" style="width: 800px; height: 600px; border: 3px solid

 

black;">

</div>

 

</body>

</html>

 

</script>

 

draw.php with the following in.

 

 

 

function draw()

{

 

// Refresh the following

var pos = new OpenSpace.MapPoint(

<?

 

$i=0;

 

$handle = fopen($file_to_use, "r");

 

$data = fgetcsv($handle, 1000, ",");

 

foreach($data as $coordinate)

{

 

echo ($coordinate);

 

if($i == 0){

 

echo ",";

 

}

 

$i++;}?>);

 

 

var marker = osMap.createMarker(pos, null);

 

//* End of refresh

}

 

basically i want to pull in the data from the draw.php however it hasnt worked.

 

 

*Note i have commented out the '<body onload="init()">' to stop loading the function within my post

 

I have since realised i dont want my draw.php (running on the server) to be writing a javascript function. I just want it to write out the data

 

Therefore my draw.php file i now just has my php code.Which returns the value from the csv file.

 

Draw.php looks as follows:

 

<?PHP

 

$file_to_use = "test2.csv";

 

 

 

$i=0;

 

$handle = fopen($file_to_use, "r");

 

$data = fgetcsv($handle, 1000, ",");

 

foreach($data as $coordinate)

{

 

echo ($coordinate);

 

if($i == 0){

 

echo ",";

 

}

 

$i++;}?>);

 

How do i now pull in that data using ajax? i have tried the following:

 

 

var url = "draw.php";

 

function draw()

{

 

if (window.XMLHttpRequest)

{// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();

}

else

{// code for IE6, IE5

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

 

 

var pos = new OpenSpace.MapPoint(

xmlhttp.open("GET",url,false);

xmlhttp.send(null);

document.getElementById('test').innerHTML=xmlhttp.responseText;

);

 

var marker = osMap.createMarker(pos, null);

 

}

 

</script>

 

Thanks for any help in advance

Archived

This topic is now archived and is closed to further replies.

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