Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding php in javascripts

Tags:

javascript

php

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.

like image 575
dbo Avatar asked Sep 07 '26 05:09

dbo


2 Answers

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

like image 154
Austin Avatar answered Sep 09 '26 19:09

Austin


Try the following

function controllerType() {
    alert('outside php');
    alert('<?php echo $message; ?>');
}
like image 32
Atticus Avatar answered Sep 09 '26 19:09

Atticus