I currently have a PHP project that uses the Zend Framework. When a user clicks a button, I am using a partial to get the HTML that needs to be added to the page. The problem is that the HTML seems to be returned as HTML rather than a string:
document.getElementById("active_projects").innerHTML='<?php echo $this->partial("project/project/activeprojectstable.phtml", array(
"headers" => $headers,
"active" => $this->active
))?>';
.innerHTML =
takes a string, but the HTML has new lines and special characters that it does not accept. How can I convert the HTML to a valid HTML string?
For a better idea of the HTML being produced by the partial that is causing the issues, this is what it evaluates to:
Try encapsulating your partial into a script
tag with type="text/template"
. Browser will not understand what type of script it is, therefore it will not execute it. Nevertheless, you can still access its contents, even if they are multiline.
<script type="text/template" id="active_projects_template">
<?php echo $this->partial("project/project/activeprojectstable.phtml", array(
"headers" => $headers,
"active" => $this->active
))?>
</script>
<script type="text/javascript">
// ...
document.getElementById("active_projects").innerHTML=document.getElementById("active_projects_template").innerHTML;
// ...
</script>
Similar pattern is being used by templates in AngularJS (type
is set to text/ng-template
).
Try html_entity_decode: http://www.w3schools.com/php/func_string_html_entity_decode.asp
<?php
$str = "<© W3Sçh°°¦§>";
echo html_entity_decode($str);
?>
The HTML output of the code above will be (View Source):
<!DOCTYPE html>
<html>
<body>
<© W3Sçh°°¦§>
</body>
</html>
The browser output of the code above will be:
<© W3Sçh°°¦§>