0

please help me with this. i have the following code that i can retrieve value from an external js file

$phpBsetB= "<script>document.write(BsetB)</script>";
echo $phpBsetB;

lets assume that BsetB stores a string "abcde". the code above will simply print out whatever the variable BsetB which is abcde. my question is, if i add the following code

$handle = fopen("test.js", "w");
fwrite($handle, $phpBsetB);
fclose($handle);

what will be saved in test.js is not abcde, instead, it saved

<script>document.write(BsetB)</script>

is there any way that i can make it saves abcde? thank you very very much for the help!

thank you guys for the quick help! i think i need to explain more of what im trying to do. maybe you guys can find a better way to solve this. i have a webpage which loads an external flat file called test.js. in the test.js, i've declared some variables in there. what i'm trying to do is, this webpage currently reads all the variables from test.js correctly, but it won't write/update test.js file. i want the webpage be able to write/update this test.js. since javascript cant write on server side, im trying to use php to write. the point is...i need to load the test.js file, so i can write/update it. my webpage currently uses js to read, so thats why my code is soo confusing now...i only know how to read js file using javascript. after i retrieve the value, i need to pass this js variable to php. or perhaps i should create the php flat file instead of js? really appreciated for your help!!!

user1865027
  • 3,505
  • 6
  • 33
  • 71
  • i think you are mixing php and javascript, the first is running on server side and the latter is supposed to run on the client side. first thing i would do is NOT name the variables with the same name its very confusing – Eyal Alsheich Dec 01 '12 at 03:34
  • i've edited, but still not working. =( – user1865027 Dec 01 '12 at 03:38

3 Answers3

2

The following line is interpreted as a string.

$phpBsetB= "<script>document.write(BsetB)</script>";

You have to create a request to send the value from client side(javascript) to the server side(php).

Ricardo Alvaro Lohmann
  • 26,031
  • 7
  • 82
  • 82
0

you are confusing php and javascript, they do not run in tendom.

your result will always be <script>document.write(BsetB)</script> because BsetB is NOT a variable, it holds NO value at all.

if you are reciveing this value from somewhere then it should look like this

$BestB = "abcde";
$phpBsetB= "<script>document.write($BsetB)</script>";
echo $phpBsetB;

$handle = fopen("test.js", "w");
fwrite($handle, $phpBsetB);
fclose($handle);
Eyal Alsheich
  • 452
  • 4
  • 12
0

PHP cant execute JavaScript functions, nor it will process the tags - thats the job for the browser. PHP will simply output what you told it to output, which is : <script>document.write(BsetB)</script>

If you want to execute that function in browser, then echo it in output or include a test.js file via script tag.

Saying that, it will produce a JavaScript error, as BsetB is not defined Javascript variable

It should be

$BsetB= "<script>document.write('abcds')</script>";
echo  $BsetB;

browser outputs abcds

OR

$my_output = "abc";
$BsetB= "<script>document.write('".$my_output."')</script>";

echo $BsetB;
Elijan
  • 1,406
  • 1
  • 8
  • 11
  • i've edited the question. can you please take a look at it again? thanks – user1865027 Dec 01 '12 at 04:16
  • @user1865027 still nor sure what you are trying to do?. You want to update a javascript file, with what? with dynamic content? if that is the case, do the javacript request (using ajax) on a server and retunr json object that can be a prased as javascript objects. In other words, don't write anythign to test.js but use javascript calls to a server that returns a values or json – Elijan Dec 01 '12 at 04:27
  • huh...thats too hard for me. ive just started web programming abt 3weeks. thanks for the help anyway! i'll try to study more be4 i can touch this area – user1865027 Dec 01 '12 at 04:51