I have the following code:
private function html_Headers()
{
$data = '
<!DOCTYPE HTML>
<head>
<script type="text/javascript">
function getOptions(chosen){
var selbox = document.myform.selectport;
selbox.options.length = 0;
if (chosen == "1") {
selbox.options[selbox.options.length] = new Option("-----------------","0");
}
}
</script>
</head>';
I’m trying to run some php mysql queries within the getOptions() JS function. I tried writing tags but it didn’t work. Does anyone see the problem here? Note that this is within a PHP class file.
JavaScript is executed on the client, while PHP is interpreted on the server.
That means: when the javascript is executed, your code is running already on the client side and PHP – Code is going to be pretty useless.
What you can do, is calling an PHP-file with a function over an AJAX-Call and parse the result back to the client. After this, you can do anything with the returned data, but you’ll have to do it within JavaScript.
For AJAX-Calls, I usually use jQuery, because the calls are easy and browser differences are handled already for you.
Of course, you can make AJAX-Calls without using jQuery, maybe you should have a look at quirksmode.org