Jump to content
xisto Community
Sign in to follow this  
chireficken

Php From File To Javascript Cant get javascript to display information from PHP file

Recommended Posts

Hey All,

I'm stuck! I am trying to use PHP variables accessed from an external file in javascript code.
I can set the array from the external file.
I can get PHP variables to javascript. I can get PHP array variables to javascript.
Something goes wrong when I try to move the array variables into javascript. I even tried setting a single variable for just one element of the array and then accessing that single variable in javascript, still no dice.

Anyway, here is my code. Any help would be VERY appreciated:

<body><?php$cal=(file("test.txt")); // test.txt is just a file with lines 1=day1 info, 2=day2 info etc on separate lines$day5=$cal[4];echo $day5; //successfully echoes '5=day5 info'?><script language="JavaScript" type="text/javascript">document.write("<?php echo $day5 ?>"); //no dice!!  Nothing comes out.  If I put in a line in the php redefining $day5='this sucks' it works fine</script><?php echo $day5; ?>  // just to check that the variable is still accessible during the javascript, and it is</body>

Share this post


Link to post
Share on other sites

I'm no PHP or java expert...but I've tried few ways and I think the reason it's not working is because it recalls only the last array value when calling within java script.

Everything else keeps the array intact but when this array is called within your java script, it only calls the last value.

Is there a particular reason why you want to call PHP value using java script? Why not just use PHP as a whole page? For example (and I know this defeats the purpose of writhing as PHP and then call it from java),

<?phpfunction foo($num){$cal=file("test.txt");echo $lines[$num];}echo "<script language=\"JavaScript\" type=\"text/javascript\">document.write(\"" . foo(3) . "\"); </script>";?>

I guess the better question is, are you trying to pass PHP variable to JAVA environment?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.