Results 1 to 3 of 3

Thread: using php in javascript

  1. #1
    Join Date
    Oct 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using php in javascript

    Hi All,
    I have a javascript file thru which I write some text on the webpage - something using the below to display on the page.

    var t = document.getElementById('foo');
    var tr = t.appendChild(document.createElement('tr'));
    tr.appendChild(document.createElement('td')).innerHTML = "NYY";
    tr.appendChild(document.createElement('td')).innerHTML = win;
    tr.appendChild(document.createElement('td')).innerHTML = loss;

    how ever I would need to write the above output on a file on a txt file on the server side. The txt file will be something like on ther server and will be appended.

    NYY 10 3

    I am not too sure how to embed php to this so that I append these outputs from javascript to the text file though php? can I pass in each output to a php function like getjsvals(team,win,loss) and then this triggers the php function to write to the file? not too sure how to accomplish this.

    I am working on a deadline, so any insights would be deeply appreciated!

  2. #2
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    In PHP on the server side (not from a webpage), you can do this if the file doesn't exist or you want to replace the contents:
    Code:
    $myFile = "/var/www/html/somewhere/somefile.dat";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $variable);
    fclose($fh);
    and if the file exists and you want to append to it, you could then:
    Code:
    $fh = fopen($myFile, 'a') or die("can't open file");
    fwrite($fh, $string1);
    fwrite($fh, $string2);
    You would first test to see if the file exists, then branch to the code part you need.....

    Reading it back usefully is not quite as simple.

  3. #3
    Join Date
    Oct 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you very much for this ... how ever how do I embed the php code with javascript. in fact

    $fh = fopen($myFile, 'a') or die("can't open file");
    fwrite($fh, $string1);
    fwrite($fh, $string2);

    the string1 and string2 are the win and loss I have here below that comes from javascript = > display the win/loss on the webpage and also write it to the file

    tr.appendChild(document.createElement('td')).innerHTML = win;
    tr.appendChild(document.createElement('td')).innerHTML = loss;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •