Javascript function not working correctly

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

      

Whatever I try, it just outputs php code as html source, not php code output. This worked fine before, and I'm not sure what I changed to lead to this behavior.

Now I have pasted all the code. The error is generated by a link that calls updateByQuery, which prevents makewindows from being divulged correctly .. I guess. I'm not sure what is wrong with updateByQuery:

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

      

0


source to share


5 answers


I assume you still have it in the file that is parsed by PHP as others have already said. Then there is probably something above this piece of code that confuses the php parser, so it doesn't recognize the php tag.

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

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

update 1 Since you were trying to insert a piece of php code and it broke, the problem is that the server is not parsing the file the way it should.

To check if the server is actually parsing your .js files (this is not the default by default) create a new file: test.js



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

      

Open the test.js file in a browser and look at the source of the page. If it has php tags, your server does not parse .js files.

update 2 If php works in .js files, try rewriting a function like this (sorry I didn't test it because I don't have access to 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(); }";    
?>

      

0


source


Have you recently migrated this file from a parsed PHP file (i.e. phtml / .php) and into a .js file? Please note that any PHP you expect to run must be parsed by the PHP parser before being delivered to the client. If it was originally in a .php file then it would be parsed / executed and work fine.



However .js files are not processed by PHP by default. They may have been there at some point, but your server admin recently updated something and lost this behavior? You might be able to use your local config file (in Apache, .htaccess) to re-enable it.

+3


source


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

+1


source


Make sure you are using the page from a web server , for example: http://localhost/yourpage.php

and not directly from the file itself:file://yourpage.php

0


source


I'm not sure if this helps, but best practices dictate that whenever you write to a new window using JavaScript, you must 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(); 
}

      

0


source







All Articles