grissom Posted March 10, 2008 Share Posted March 10, 2008 Hi all Please help !! I'm trying to pass a PHP variable into Javascript with weird results. For example, *THIS* bit of code works (as you would expect) : <?php $foo = file_get_contents("helloworld.txt"); $bar = "goodbye everybody"; ?> <HTML> <HEAD> <TITLE>A php JS test with bar</TITLE> </HEAD> <BODY> <SCRIPT type="text/javascript"> s = "<?php echo $bar; ?>" alert(s) </SCRIPT> </BODY> </HTML> however *THIS* refuses to work and says "done with errors on page". : <?php $foo = file_get_contents("helloworld.txt"); $bar = "goodbye everybody"; ?> <HTML> <HEAD> <TITLE>A php JS test with foo</TITLE> </HEAD> <BODY> <SCRIPT type="text/javascript"> s = "<?php echo $foo; ?>" alert(s) </SCRIPT> </BODY> </HTML> Why on earth should it work with bar and not with foo ???? For info, the file "helloworld.txt" is a text file containing the words "hello world" (if ya couldn't have guessed it) This is driving me mad, please help ... bob Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/ Share on other sites More sharing options...
soycharliente Posted March 10, 2008 Share Posted March 10, 2008 I thought I had an answer. I was wrong. I had to change this after I posted it already. Never mind. Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488849 Share on other sites More sharing options...
grissom Posted March 10, 2008 Author Share Posted March 10, 2008 Yeah, it's a real head-scratcher. Needless to say, my *real* code is a bit more complex than this, but I've distilled the problem down into those couple of examples. It seems that any variable I define explicitly is easily picked up in the JS, however if I create a variable out of the text file, the JS refuses to acknowledge its existence. Even though the PHP acknowledges it does exist (as can be verified by doing an echo command inside the php) AAAAAARGH !! Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488887 Share on other sites More sharing options...
uniflare Posted March 10, 2008 Share Posted March 10, 2008 um, have you tried running the examples you created in your post through php? cuz they work on mine, its not code/syntax problem. in your complex script give us the html source code and show us where exactly the variable is in the js code. hopefully it will help us debug this situation Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488906 Share on other sites More sharing options...
grissom Posted March 10, 2008 Author Share Posted March 10, 2008 Thanks for all your help guys, I loaded the two scripts (and the helloworld.txt file) to my server. This file runs okay : www.catmanplus.com/bar.php and THIS one gives me a javascript error : www.catmanplus.com/foo.php I'd be interested to know how they run on your PC. BTW, I'm running internet explorer and Vista Cheers ! Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488918 Share on other sites More sharing options...
Northern Flame Posted March 10, 2008 Share Posted March 10, 2008 try this: <HTML> <HEAD> <TITLE>A php JS test with foo</TITLE> </HEAD> <BODY> <SCRIPT type="text/javascript"> var s = "<?php echo $foo; ?>" alert(s) </SCRIPT> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488923 Share on other sites More sharing options...
grissom Posted March 10, 2008 Author Share Posted March 10, 2008 Hi Northern flame Thanks for the advice, but it didn't make any difference. P.S. I've tried out my scripts on my daughter's PC which runs Netscape navigator and as before, bar runs fine and foo doesn't. I've tried all kinds of permutations of things and now I'm getting really stumped ! Please help me keep trying ! many thanks Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488926 Share on other sites More sharing options...
uniflare Posted March 10, 2008 Share Posted March 10, 2008 are you sure javascript can accept newlines like that in strings... no alert box popped up for me... instead of making it read: <SCRIPT type="text/javascript"> s = "Hello World" alert(s) </SCRIPT> try making it read: <SCRIPT type="text/javascript"> s = "Hello World" alert(s) </SCRIPT> and c if that works Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488952 Share on other sites More sharing options...
Northern Flame Posted March 10, 2008 Share Posted March 10, 2008 or if u want a new line do this: <SCRIPT type="text/javascript"> s = "Hello\nWorld" alert(s) </SCRIPT> Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488959 Share on other sites More sharing options...
BlueSkyIS Posted March 10, 2008 Share Posted March 10, 2008 i agree. i don't think Javascript will handle a line break in a quoted string. Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488963 Share on other sites More sharing options...
Northern Flame Posted March 10, 2008 Share Posted March 10, 2008 i agree. i don't think Javascript will handle a line break in a quoted string. if you use \n it will, ive done that before and it worked fine, but if you enter the line break by hitting the "enter" key it will not support it. Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488970 Share on other sites More sharing options...
BlueSkyIS Posted March 10, 2008 Share Posted March 10, 2008 sorry, yes that's what i meant. i have used \n, but it has to be represented that way. a plane carriage return won't work. Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488972 Share on other sites More sharing options...
bals28mjk Posted March 10, 2008 Share Posted March 10, 2008 @grissom That's odd, JavaScript accepts endlines. I'd just include the txt file in a hidden form and grab the values with JavaScript. <HTML> <HEAD> <TITLE>A php JS test with foo</TITLE> </HEAD> <BODY> <SCRIPT type="text/javascript">; onload=function() { alert(document.getElementsByTagName("input")[0].value); } </SCRIPT> <input type="hidden" value="<? include("helloworld.txt"); ?>"> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-489008 Share on other sites More sharing options...
corbin Posted March 10, 2008 Share Posted March 10, 2008 s = "Hello World" alert(s) As already stated, JS ain't a big fan of line breaks just plain in there..... You could do s = "Hello\n World" Or, as already said: s = "Hello\nWorld" Anyway, let's say $s is the php variable with the file contents. You could just replace line breaks with \n. $s = str_replace(PHP_EOL, '\\n', $s); Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-489011 Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 i agree with corbin, $foo = str_replace(PHP_EOL, '\\n', $foo); should do it.. you may even preg_replace to clean out everything that will mess it up.. Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-489035 Share on other sites More sharing options...
grissom Posted March 11, 2008 Author Share Posted March 11, 2008 Thanks guys !! it worked !! Originally my code was using ......while (!feof) ... $s[$i] = fgets... to read a text file into a string array one line at a time - and I was getting the same frustrating error !! Problem is, when I printed the php variables on the screen from within php using echo, there didn't seem to be any carriage return characters in there - but there were ! And this caused the javascript to throw a huge wobbler. MadTechie's string cleanup of the invisible characters worked a treat ! Many thanks to everyone who contributed on this and pointed me in the right direction, I would have been tearing my hair out for days without your help. Quote Link to comment https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-489308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.