Use variable php in java

Hello for all,

i have 2 page the first of page.php and the second of page.js

I want to use the variable which i got from page php in the page js


$De = example::model()->find('id = :Id', array('Id' => $_POST['V.dropdownlist']));

$origin = $De->Origin;

$des = $De->Destination;

$from = $De->orgin_name;

$to = $De->destination_name;

and in js :

var origine = "<?= echo $origin;  ?>";

var destination = "<?= echo $des; ?>";

var from = "<?= echo $from; ?>";

var to = "<?= echo $to; ?>"; 





        type: "POST",

        url: getHost() + "example/...",

        data: {

            destination: destination,

            origine: origine,

            from : from,

            to : to,

            start: start,

            end :end


        dataType: "text",

        success: function (xy) {





But don’t work this variable : destination, origine , from, to

really i don’t know why

PLEASE can you help me how to use this variable php in page.js ?


It’s javascript not Java. Significant difference :). I guess your problem lies in a way you are echoing your variables. Try changing short php tags into standard ones. Like that:

var to = "<?php echo $to; ?>"; 

or remove echo and leave just short tag/echo shortcut like that:

 var to = "<?= $to; ?>"; 

Also, you are trying to use php in .js file. Make sure your server is configured to parse .js as it was php. Even better, change js into php. If you have lot of js code, you can define js variables in your view file, and include your js file at the bottom of your page.

Notice, that short tags have to be turned on in php.ini for that to work (as of php 5.3, 5.4 will allow for short echo even with short tags turned off) and that’s not common on shared hosting servers.

Thank you but really not work this code.

No answer :s

Please help me.

how to use this variable php in page.js OR how to get directly this variables ($origin = $De->Origin;

$des = $De->Destination;

$from = $De->orgin_name;

$to = $De->destination_name; ) in page.js


????? ::)

No HELP ????

jajaja =) you cant use php tag in a javascript file =).

freeyii, as I wrote in my first post, and as sensorario stated right above, make sure php actually parses your file. It would be really helpful if you wrote what you actually tried to do to solve your problem instead of bumping up your thread… Once again:

Did your tried moving your js code to php file ? Did you look into documentation? CClientScript has some nice functions which can help you:



You can use first one to include variables you echo from php to js in .php file, and second function to include js file where variables set before will be used…