I have a PHP function that queries a MySQL database and outputs an html table. This works fine with following PHP code:
<?php
showTable();
?>
But if I use the javascript code:
form.onsubmit = function() {
document.getElementById("php_code").innerHTML="<?PHP showTable(); ?>";
};
the browser does not show the table. There seems to be a problem with the (innerHTML) html string. I noticed for example that a carriage return inside a MySQL parameter causes problem with the innerHTML, but not with the PHP-only code.
Is there a way to fix this?
(the reason I wish to use the javascript bit is to be able to have a form with two buttons with different PHP functions depending on the form buttons clicked).
You need to escape the output of showTable() properly if you want to do it in a variable like that.
However, I propose a different method for you. Include the HTML like this:
Notice the use of type=”text/html” in the previous script tag. This will cause the table not to be shown to the browser, but retrievable by javascript.
Good luck.