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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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