You have a *MAJOR ERROR* in your code that has nothing to do with PHP interaction!!
Let's take your code and remove the PHP, so it just looks like this:
Code:
<p><button onclick="myFunction()">Show the text</button></p>
<script>
function myFunction()
{
var unedited_text = 'ANY TEXT AT ALL';
document.write(unedited_text);
}
</script>
Yes, that code will work.
*BUT*, when the user clicks on that button, the *ENTIRE HTML PAGE WILL DISAPPEAR*, to be replace with *ONLY*
Even the JavaScript that was used to do the document.write will be gone.
Why? Because if you use
document.write *OTHER* than during the CREATION of the HTML of the page, you *automatically* first do the equivalent of
window.open( ) of the current page, which means you are WIPING OUT all content of the page and replacing it with whatever
document.write creates.
This is only *ONE* of the reasons that
document.write is considered very very obsolete! There are a very few reasons for using it, but they are much more advanced than have anything to do with responding to a mouse click or similar. As a good rule: Until and unless you ever discover that there is NO OTHER WAY to accomplish what you want, do *NOT* use document.write. And of course the same applies to
alert( ) and
confirm( ).
****************
On a completely different topic:
There is no practical difference between
Code:
echo "$words_unedited";
and
Code:
echo $words_unedited;
The quote marks in the first case will simply disappear. So the end result in HTML will be identical.
**********
SO...
A corrected version of your function might be something like this:
Code:
<p><button onclick="myFunction()">Show the text</button></p>
<p id="theText"></p>
<script type="text/javascript">
function myFunction()
{
var unedited_text = '<?php echo $words_unedited;?>';
document.getElementById("theText").innerHTML = unedited_text;
}
</script>
If you aren't sure that you PHP code is doing the right thing, then DEBUG DEBUG DEBUG.
In this case, the FIRST thing to do is bring up the web page in your browser. Then:
-- click on the VIEW menu of your browser
-- click on the SOURCE or PAGE SOURCE menu item
-- look at the HTML that appears, which is the HTML *as the browser sees it*.
-- find your
myFunction code in that HTML
-- see if it looks like it is, indeed, getting the expected string from PHP.