views:

170

answers:

5

hello,

I have this little function

function makewindows(){
child1 = window.open ("about:blank");
child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");
child1.document.close(); 
}

Which whatever I try, simply outputs the php code as the html source, and not the result of the php code. This was previously working fine, and I am not sure what I have changed to result in this behavior.

I have pasted all the code now. An error is generated by a link that calls updateByQuery, preventing makewindows from being parsed correctly..I think. I am not sure what is wrong with updateByQuery however:

function updateByQuery(layer, query) {
   url = "get_records.php?cmd=GetRecordSet&query="+query+"&sid="+Math.random();
   update(layer, url);
}
+1  A: 

This code must be in a file that is parsed by PHP before being sent to the browser. Make sure it has a ".php" extension (or that Apache/(or other) is configured to put whatever extension it is using through PHP). Also, make sure PHP is installed correctly and working.

Sydius
This JavaScript file is included in index.php, and previously worked fine. The other javascript functions and php files all work fine.
Joshxtothe4
+2  A: 

Have you recently moved this file out of a PHP parsed file (i.e. .phtml/.php) and into a .js file? Note that any PHP you expect to be executed must be parsed by the PHP parser before delivery to the client. If it was originally in a .php file, then it would have been parsed/ executed, and worked fine.

However, .js files are not, by default, parsed by PHP. Perhaps they were, at one point, but your server administrator has recently upgraded something, and lost this behaviour? You may be able to use a local configuration file (in Apache, .htaccess) to re-enable it.

Adam Wright
it was always in a .js file that was loaded from a php file, that has not changed.
Joshxtothe4
Then my money is firmly on a recent reconfiguration by your administrator, preventing .js files from being parsed by PHP
Adam Wright
Yes, the problem is that it's not getting parsed.
some
I am the administrator, and have not changed anything.
Joshxtothe4
A: 

Make sure you are running the page from the webserver like such: http://localhost/yourpage.php and not directly from the file itself like such: file://yourpage.php

Andreas Grech
file is definitely being served
Joshxtothe4
+1  A: 

I assume you still have it in a file that is parsed by PHP, like the others already have said. Then it is probably something above this code snippet that confuses the php-parser so it don't recognize the php-tag.

To test that, try to output something else before this function, maybe just a comment or something.

Also, use "var" before client1, or else client1 will be in the global scope.

update 1 Since you tried to insert a piece of php-code and it broke, then the problem is that the server don't parse the file as it should.

To test if the server really parses your .js files (its not the default setting I believe), create a new file: test.js

<?php echo "This is a test"; ?>

Open the test.js file in your browser and look at the page source. If it has the php tags your server don't parse .js files.

update 2 If the php works in .js files, try to rewrite the function like this (sorry I have not tested it because I don't have access to a php-server right now)

<?php    
echo "function makewindows(){var child1 = window.open (\"about:blank\"); " .
"child1.document.write(\"" . htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES) . "\");" . "child1.document.close(); }";    
?>
some
I called an alert() before the function and it worked fine.
Joshxtothe4
It sounds like you just put an alert there, you need to use php to do that. like <?php echo "alert('php test');" ?>
some
The problem is NOT with the javascript, because the javascript don't parse php. The problem is that for some reason your php-parser on the server don't parse the php as it should.
some
Right now I don't have access to a php-server so I can't test the code.
some
Putting in a PHP alert definitely breaks it, and causes an error on a link that calls a function above makewindows. I will paste that code now.
Joshxtothe4
The PHP code is parsed and run on the server, the javascript on the client. If the php code comes to the client, then the server hasn't parsed the PHP...
some
some php code is working, just not the php code in makewindows
Joshxtothe4
there seems to be a problem after updateByQuery, as query which is a php function is passed fine, but anything after such as makewindows fails.
Joshxtothe4
Sounds like something is confusing the php-parser... Several years ago I had a similar problem with another language. Then it was because of an open quote or that I antecedently had put some weird characters that the parser didn't like.
some
You could try to solve the problem like this: Create a new file and add a piece of php-code in it and test that it works. Then copy part for part from the original file to the new file, and see where it stops working.
some
It seems to be receiving variables passed from php without issue, but will not parse php. The rest of the site is .php files and parses fine.
Joshxtothe4
Are you saying that the "*.php" files are parsed but not ".js" ?
some
yes, .js files are passed as javascript though. I think this problem has nothing to do with the server configuration though, as it is easyphp and has not changed, only the file has changed
Joshxtothe4
Just for the fun of it, change the name to something.php and look at the file in your browser to see if it get parsed the right way.
some
yes, it works perfectly if I do that, the source is shows the contents of artcile_desc
Joshxtothe4
Great. Now you problem is to configure your webserver to parse the .js files. I don't see that you have specified what webserver or what platform you are on. One way is to use .htaccess files, and maybe you have accidentally deleted the one you had.
some
Normally the .htaccess file is hidden, so if you have copied your files from one place to another, you might have missed that configuration file.
some
I am using easyphp on windows for the moment. I still this this is a problem in th code, as I have not touched the server configuration, and php in javascript was previously working fine.
Joshxtothe4
Since it works if you have it as ".php" and not when you have it as ".js", its nothing wrong with the file, but with the configuration of your server. I'm not familiar with easyphp, but since it uses apache it shouldn't be a big problem.
some
Search for "apache php parse" and you get several suggestions
some
eaysphp will parse php in javascript automatically. It is definitly a problem with my code, and I have editied my post to show the entire code. Changing the configuring to ensure that .js is parsed fixed nothing.
Joshxtothe4
A: 

I'm not sure if this will help, but best practices dictate that whenever you write to a new window using JavaScript, you should open and close the document. Can you try this?

function makewindows(){
  var child1 = window.open ("about:blank");
  child1.document.open();
  child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");
  child1.document.close(); 
}
cLFlaVA
@cLFlaVA: The problem is that the PHP code don't get parsed, and that don't depend if the document is open or not. But good that you pointed that out. Would you like to edit your answer and put a "var" before the first "child1" to make the variable local instead on global?
some
Good point; thanks :) Although, based on the small code snippet provided by the OP, child1 *may* have been declared globally for a reason.
cLFlaVA