Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/95498-php-to-javascript/
Share on other sites

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 !!

Link to comment
https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488887
Share on other sites

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 ;)

Link to comment
https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488906
Share on other sites

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 !

Link to comment
https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488918
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488926
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-488952
Share on other sites

@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>

Link to comment
https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-489008
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-489011
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/95498-php-to-javascript/#findComment-489308
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.