View Full Version : urgent help needed in javascript

03-06-2012, 07:43 AM
I tried to write the variable my_var into a text file but it only shows null after executing the code. Can anybody help pls?? Below is my code

<script language="javascript">
var my_var

function WriteToFile()

my_var = 123;

document.write(<?php $file="file.txt";
$fh = fopen($file, 'w') or die("can't open file"); ?>;);
document.write(<?php $stringData = my_var ?>;);
document.write(<?php fwrite($fh, $stringData); ?>;);
document.write(<?php fclose($fh); ?>;);
<body onLoad="WriteToFile();">
<p>Hello World</p>


03-06-2012, 07:45 AM
urgent help needed in javascriptwhen do you need it by? - no point posting if the deadline has passed.

And since it's urgent, you've probably posted on other forums as well.

Have you received a solution from other websites yet?

03-06-2012, 07:58 AM
not yet and i only posted it in this forum

03-06-2012, 08:16 AM
so when's your deadline?

Philip M
03-06-2012, 08:37 AM
If you want people to spend time and trouble answering your question you really must read the forum rules and posting guidelines, especially those about silly thread titles and the use of code tags (surely it is obvious why those are needed?). webdev1958 is trying to tell you that your post is not urgent for us, and must await its proper turn. Why do you think that you are entitled to priority?

Edit your post and then perhaps someone will answer you.

It is your responsibility to die() if necessary….. - PHP Manual

03-06-2012, 08:54 AM
What a jumbled mess of PHP and antiquated JavaScript.

<script language="php"> is a valid script tag as is <script type="application/javascript"> although since IE8 and earlier don't support JavaScript people usually use the deprecated <script type="text/javascript"> that allosw the script to run as JScript in those browsers.

Since you have PHP available you have no need to use document.write in JavaScript since the PHP print or echo command does the exact same thing and effectively runs at the same time - before the page finishes loading.

When combining PHP and JavaScript the PHP runs first and so must construct valid JavaScript statements that can run after.