There isn't a whole lot of benefit of writing javascript inside of a php, typically that would just lead to unnecessary coupling of code. For future note however, it is possible to define javascript from PHP or any other server side language in a couple of ways.
The first would be simply striping the script tags from your php and calling eval from javascript:
ajax.php
<?php
echo "var e = document.getElementById('widget_more_');\n";
echo "e.innerHTML += '<p> <a>TEST</a> </p>';\n";
?>
Javascript
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//For function scope
eval(xmlhttp.responseText);
//For global scope
window.eval(xmlhttp.responseText);
}
}
Alternately a "slightly" more flexible solution:
ajax.php
<?php
echo "function showTest(){";
echo " var e = document.getElementById('widget_more_');\n";
echo " e.innerHTML += '<p> <a>TEST</a> </p>';\n";
echo "}";
?>
Javascript
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//Now we will dynamically create your script element in the header
//Get the header element on the page
var head= document.getElementsByTagName('head')[0];
//Create the new script tag
var script= document.createElement('script');
script.type= 'text/javascript';
script.innerHTML = xmlhttp.responseText;
//Append the new script to the header
head.appendChild(script);
showTest();
}
}
These examples are more for academic purposes than practical (although dynamically loading javascript has it's uses, see the google ajax api's). Both of these examples still leave your code heavily coupled. Midas's answer is a much more proper way of accomplishing what you are after.
xmlObj.responseTextas plain text, not doing anything. Because of the script tags, it seems like it would be more difficult than necessary to append it to the body of your HTML, and eval() won't work.