nonaz Posted November 19, 2013 Share Posted November 19, 2013 Hi, im new to PHP and tried to do the following in my .js file: function ChangeTextbox(){ setTextBox(' <?php include "exampledatafile1.txt" ; ?> ');} It doesn't seem to work... Why? I expected the server to substitute the php tag with the text from my file. Instead the textbox gets the php statement itself. Is there an error? Is it because its a .js file, and my server doesn't look through that? Sorry if it's stupid, explanations are very welcome! Quote Link to comment Share on other sites More sharing options...
MDCode Posted November 20, 2013 Share Posted November 20, 2013 PHP can not be parsed in a js file unless you configure your server to. Even then, PHP is server-based and will parse before js. So, It would be like setTextBox(-contents of file will be here-); Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted November 20, 2013 Share Posted November 20, 2013 To pass the information to the .js file, you could try something like the following. PHP script <html> <head> <title>Text File Input Test</title> <script type="text/javascript"> var textFileInfo = "<?php echo file_get_contents('myTextFile.txt'); ?>"; </script> <script language="javaScript" type="text/javascript" src="myJSCode.js"></script> </head> <body> </body> </html> myJSCode.js alert(textFileInfo); myTextFile.txt hello world Quote Link to comment Share on other sites More sharing options...
nonaz Posted November 20, 2013 Author Share Posted November 20, 2013 (edited) PHP can not be parsed in a js file unless you configure your server to. Even then, PHP is server-based and will parse before js. So, It would be like setTextBox(-contents of file will be here-); Yes that is exactly what I want to do. The txt file is only accessible locally on my server, and the client browser should never see the php command. However, I can't figure out how to set the php server to read .js files too. Edit: It sounds like overkill maybe, but if it's easy it might be very nice, since I don't use a CMS but would like to simulate some of it's functionality (server-side). To pass the information to the .js file, you could try something like the following. PHP script <script type="text/javascript"> var textFileInfo = "<?php echo file_get_contents('myTextFile.txt'); ?>"; </script> <script language="javaScript" type="text/javascript" src="myJSCode.js"></script> This was a nice idea. It didn't work because the text file has newlines in it that are not escaped. I fixed it like this: Index.php: <script type="text/javascript">var textFileInfo= "<?php echo str_replace(array("\r\n","\n","\r"),"\\n\"+\"",file_get_contents('exampledatafile1.txt')) ?>";</script> <script language="javaScript" type="text/javascript" src="myJSCode.js"></script> Which works nicely like it should. The only thing is my index.php will become very ugly once I upload all the files this way. And it would be nicer to mod the filename in the script that uses it. Well, it works, I'm happy enough with this, but any suggestions to make it nicer/prettier are very welcome, I'm still trying to learn. Thanks a lot for the answers!!! Edited November 20, 2013 by nonaz Quote Link to comment 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.