I was trying to embed some PHP into a javscript function that gets called from an onChange in a drop down list. the javascript function is in a .js file, I can get the DDL to call the function, however i cannot get the function to work with the php... eventually i am going to use the value selected in the DDL to access a DB and populate other fields from there, right now i am trying to get it to work with the php:
function controllerType(){
alert('outside php');
<?php
$message = "inside php";
echo "alert('$message');";
?>
}
The function prints the first alert but not the alert called in the php.
Your webserver probably isn't looking for PHP code in .js files by default. You'll have to tell it to either look for PHP in those files or change the file extension to .php.
If you you want to try the former and you're running an Apache web server, try adding the following line to your .htaccess file:
AddHandler application/x-httpd-php .js
Try the following
function controllerType() {
alert('outside php');
alert('<?php echo $message; ?>');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With