passing php variables from controller to javascript

hi,

how can i pass php variables value from the controller to javascript?

it depends on the user case… can you post your controller / view code and explain what you need to pass where…

If it is an Ajax call, I suggest you to encode/decode it using JSON.

i have the same question.

how to decode an array in controller plz answer with example I shall be very thankful to u

Here few simple examples:




// php

class Controller ...

	public function actionDo() {

		$this->render('do', array(

			'number' => 123, // just numeric value

			'string' => "my string with \'\"", // string

			'array' =>  array("prop1" => "val1", "prop2" => "val2"), // array

			'model' => Post::model()->findByPk(1), // CActiveRecord instance

		));

	}

	...

	

// view

<script>

	var my_number = '<?php echo CJSON::encode($number);?>'; // number

	var my_string = '<?php echo CJSON::encode($string);?>'; // string

	var my_array = '<?php echo CJSON::encode($array);?>'; // array; you'll get object - {prop1: "val1", prop2: "val2"}

	var my_model = '<?php echo CJSON::encode($model->attributes);?>'; // model; you'll get object - {id: 1, some_attribute: "attribute_value", ...}

</script>