Log in

View Full Version : using php in javascript



vijayrajesh98
11-12-2008, 05:24 PM
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!

Strangeplant
11-13-2008, 04:48 PM
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:
$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:
$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.

vijayrajesh98
11-14-2008, 08:05 PM
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;