Jump to content

simple php bug/question


nonaz

Recommended Posts

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!

 

 

 

 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 by nonaz
Link to comment
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.