Jump to content

passing vairables from php to javascript


inkexit

Recommended Posts

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>

 

 

 

 

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.