Passing Variable From One View To Other Using Post Method

I am trying to use POST method to pass variable from one view to other view in following way

in 1st view file


<script>

	function testing(col) {

		$("#bookId").val(col);

		var csrfTokenName = <?= CJavaScript::encode(Yii::app()->request->csrfTokenName); ?>;

		var csrfToken = <?= CJavaScript::encode(Yii::app()->request->csrfToken); ?>;

		var postParams = {"ad_id":col};

		postParams[csrfTokenName] = csrfToken;

		

		$.ajax({

			type: 'POST',

			url: "<?php echo Yii::app()->createUrl('siteaccess/hello'); ?>",

			data: postParams,

			dataType: 'text',

			success: function(col){console.log(col)},

		});

	};

	</script>

To get variable in other view file I do it something like this


$id = Yii::app()->request->getPost('ad_id');

echo $id

But in the webpage I am not able to see the result of echo while at the same I am able to see value being echoed in firebug. Whys is this happening?

Also var_dump($id) is showing NULL while firebug shows string(15) "357804043678014" which is correct. Why is this happening?

Not quite clear what you’re trying to do, because now it works just as expected.

Probably you want something like


success: function(col){$("#bookId").val(col);}

My problem is I want to pass $id to some other function $this->func($id) after $id = Yii::app()->request->getPost(‘ad_id’); but it is passing NULL value.

So basically, you’re telling that




$id = Yii::app()->request->getPost('ad_id');

echo $id; // Here $id is defined (you've missed ";" btw)

var_dump($id); // And here $id is not defined



right?

Since this situation is hardly possible, paste some real code to see what’s actually happening.


$id = Yii::app()->request->getPost('ad_id');

$fuel = $this->fuel($id);

var_dump($fuel);

I want to do some thing like this but var_dump(fuel) shows NULL while in firebug it shows 400 which is correct.

In other view file I have cgridview and when I click on one of the rows I passed $data from that view file using POST. Following is my click code in cgridview


'click' => 'function(){testing($(this).parent().parent().children(\':nth-child(2)\').text());

where testing is javascript function mentioned in original post.

So how $this->fuel() looks like?


public static function fuel($siteID) {

$connection = Yii::app()->db; 		

$sql = "SELECT sum(fuelLevel) from datatable where siteID = :site and arrival_time between :up and :low"; 		$command = $connection->createCommand($sql); 		

$command->bindValues(array(':site'=>"$siteID", ':up'=>'2013-11-22 00:00:00', ':low'=>'2013-11-22 23:59:59')); 		 		return $command-> queryScalar(); 	

}

A function in controller file.