Page 1 of 1

passing variables from txt file to php o javascript???

Posted: Mon Jul 05, 2010 7:38 pm
by inkexit
I have a text file (test.txt) that is being uploaded to my x10 premium site via another program on my home pc. The file is technically in CSV format, but as of right now all it has in it is one integer. I've been working on the code posted below for the html, the javascript, and the php, but am running into a few walls.

To understand the whole concept, this is what I'm trying to do: I'm trying to have the javascript check the txt file via php file every 10 seconds, and only refresh the data on the html page if the integer saved in text.txt has changed.

I'm not sure how to pass a variable from php to the javascript. I could use echo and just have the php 'print' out the integer, but eventually test.txt will have a lot more info in it and I'm gonna have to pass a bunch of variables inbetween the javascript and the php anyway.

Also, this is especially ignorant of me, but I'm not sure how to call a javascript function every 10 seconds.


Php
<?php

$handle = fopen("http://www.MySite.com/Signal/test.txt", "r");

while (($data = fgetcsv($handle, 1, ",")) !== FALSE)
{
$PHPSignal=$data[1];
echo $PHPSignal;
echo "</br>";
}

?>


javascript:

// JavaScript Document


var response;
var request = false; // The object to handle the request for data
var reqType = "GET"; // Make the request type a GET as opposed to a POST
var url = "http://www.MySite.com/ReadCSV.php";
var asynch = true; // Make this an asynchronous request
var interval = 1000 * 1;    // Update the page at X second intervals



function getData()
{
    //
    // Do the first request, then
    //
    httpRequest();
   
    //
    // send the request at intervals
    //
    setInterval(httpRequest, interval);
}



/* Wrapper function for constructing a Request object.*/
function httpRequest()
{
    //
    // Only create the request object.
    //
    //Mozilla-based browsers
    if(window.XMLHttpRequest)
{
        request = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // IE browsers
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request)
        {
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
     }
    //the request could still be null if neither ActiveXObject
    //initializations succeeded
    if(request)
    {
       initReq(reqType,url,asynch); // Initialize the request
    } 
    else
    {
        alert("Your browser does not permit the use of all "+
        "of this application's features!");
    }
}



/* Initialize a Request object that is already constructed */
function initReq(reqType,url,bool)
{
    /* Specify the function that will handle the HTTP response */
    request.onreadystatechange = handleResponse;
   
    //
    // In order to prevent IE browsers from returning a cached version of
    // the XML file we to append a unique value to the end of the URL. This is
    // ignored by the responder script; but ensures the page gets updated.
    //
    var urlToSend = url + "?key=ms" + new Date().getTime();
    request.open(reqType, urlToSend, bool);
 
    request.send();
}

//event handler for XMLHttpRequest
function displayDocInfo(doc)
{
if (request.readyState == 4)
{
if (request.status == 200)
{
var response = request.responseText.split("|");
document.getElementById("order").value = response[0];
document.getElementById("address").innerHTML = response[1].replace(/\n/g, "");
} else
{
alert("status is " + request.status);
}
}
}



html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ThumbScalp.com - The best realtime signals of the market.</title>


<SCRIPT language="JavaScript" SRC="/ThumbJavascript.js"></SCRIPT>


</head>


<body>
<div>


<A HREF="javascript:getData()">Click to update.</A>

</div>
</body>
</html>

Re: passing variables from txt file to php o javascript???

Posted: Mon Jul 05, 2010 7:54 pm
by Jonah Bron
Why not just store the integer in a regular text file? Then just call the text file directly with Ajax.

Code: Select all

function getUpdate() {
    var ajax_obj = // use XMLHttpRequest or whatever
    ajax_obj.open('get', 'test.txt', true);
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4) {
            document.getElementById('update_box').innerHTML = ajax.responseText;
        }
    }
    ajax.send(null);
}
var repeat_function = setInterval(getUpdate, 10000);

Re: passing variables from txt file to php o javascript???

Posted: Mon Jul 05, 2010 8:27 pm
by inkexit
OK. That looks good. I'm just not sure how to call the functions in the html. Right now I'm doing it as a clickable anchor tag. I see you wrote another function with an intervals to check the file. But once the html page has loaded, what keeps firing off that function? Wouldn't I have to make an infinite for loop with a timeout in it or something?

Re: passing variables from txt file to php o javascript???

Posted: Mon Jul 05, 2010 8:29 pm
by Jonah Bron
Take out the last line I gave you, and use it like this:

Code: Select all

...
<body onload="var repeat_function = setInterval(getUpdate, 10000);">
...

Re: passing variables from txt file to php o javascript???

Posted: Mon Jul 05, 2010 10:23 pm
by inkexit
Thanks, Jonah