Yii Framework Forum: Use variable php in java - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Use variable php in java Rate Topic: -----

#1 User is offline   freeyii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-April 11

Posted 25 October 2011 - 06:28 AM

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

page.php

$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; ?>"; 
////
///
///

    $.ajax({
        type: "POST",
        url: getHost() + "example/...",
        data: {
            destination: destination,
            origine: origine,
            from : from,
            to : to,
            start: start,
            end :end
        },
        dataType: "text",
        success: function (xy) {
            $('#site').html(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 ?

Thanks
0

#2 User is offline   sidewinder 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 08-July 09
  • Location:Poland

Posted 25 October 2011 - 06:48 AM

View Postfreeyii, on 25 October 2011 - 06:28 AM, said:

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



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.
---------------------------------------------------------------------
"Never memorize what you can look up in books."
Albert Einstein
0

#3 User is offline   freeyii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-April 11

Posted 25 October 2011 - 07:00 AM

View Postsidewinder, on 25 October 2011 - 06:48 AM, said:

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

or remove echo and leave just short tag/echo shortcut like that:
 var to = "<?= $to; ?>"; 




Thank you but really not work this code.
0

#4 User is offline   freeyii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-April 11

Posted 25 October 2011 - 08:48 AM

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


Thnx
0

#5 User is offline   freeyii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-April 11

Posted 25 October 2011 - 11:01 AM

????? ::)
0

#6 User is offline   freeyii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-April 11

Posted 25 October 2011 - 11:30 AM

No HELP ????
0

#7 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,987
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 25 October 2011 - 01:03 PM

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

#8 User is offline   sidewinder 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 08-July 09
  • Location:Poland

Posted 25 October 2011 - 04:57 PM

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:

Quote

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.


Did your tried moving your js code to php file ? Did you look into documentation? CClientScript has some nice functions which can help you:
registerScript()
registerScriptFile()
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...
---------------------------------------------------------------------
"Never memorize what you can look up in books."
Albert Einstein
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users