How To Use Post Or Get To Pass Variable From One View To Other Viiew

How can I pass a variable from javascript view to php function in other view. I tried using POST and Get method using ajax but it is not working. What are the other options I have?

Can you write down the code? or explain little bit more?

In one of the view I have a javascript code and in this code their is a variable which I want to pass to php code in other view page.

Hi,

Say first view file myview1.php contains




<script>

var x='mytext';

</script>



You can pass through a link like




window.location.href = 'yoururl?var='+x;



and get the value in other view file using getQuery(‘var’);

I think I am right. Lets cheers!

u can’t pass js variable x in page_a to a php function y in page_b because x is in the browser side and y is in the server side.

You need to parse the value in javascript, then do a GET request to the next page. Building on the other examples given, that means you want to parse out the value and pass it to the next page:

<script>var x = ‘whatever-value-you-have-in-js’; window.location = ‘step2.php?x=’+x;</script>

The important notes above - you must be able to get at the var x inside the script, which will allow you to pass it using GET to the step2.php page. You will know it works when you see a URL of "step2.php?x=whatever-value-you-have-in-js"

Yes of course. Also you cannot call the php function from client side script. So only I suggested using GET method get the value inside your php function. I think I am doing right?. If wrong please suggest some sample code let us come with your solution.

Thanks for all of your reply. Is it possible to do it using POST method? If yes then how?

Only possible using ajax

check the request isAjax under the action get post query values and call the php function… and do your stuffs

Cheers!

I did something like this using POST. In view1.php I want to pass col to view2.php. view1.php code


<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 $this->createUrl('siteaccess/hello'); ?>", 			

 data: postParams, 		

 }); 	

}; 	

</script>

view2.php code




<?php

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

var_dump($id);

?>



But this is not working. For var_dump($id) I get NULL. Can you show me how to get it working.

Oh you have to use this




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



Before checkout your ajax url. You can check in the action by putting




print_r($_POST); die;



I have tried getParam but it also doesn’t work and print_r($_POST) shows Array(). But in firebug or google chrome I am able to see that post is working. Following is output of inspect element of google chrome

ad_id:357804043678014

YII_CSRF_TOKEN:da51cd4e6f75ef1c77708d967df35bd2

Response Headersview source

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Connection:Keep-Alive

Content-Length:6800

Content-Type:text/html

Date:Wed, 27 Nov 2013 06:20:33 GMT

Expires:Thu, 19 Nov 1981 08:52:00 GMT

Keep-Alive:timeout=5, max=100

Pragma:no-cache

Server:Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16

X-Powered-By:PHP/5.4.16

Can you help me where I maybe making mistake?

Please checkout your ajax url

when using ajax, ur view2.php receive ajax request and return output to ajax page, and the whole process finish. so u can see view2.php’s return value in ur view1.php’s callback function(the success part).

try this:

view1.php code:




<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 $this->createUrl('siteaccess/hello'); ?>",                    

 data: postParams,  

 //...see here...

  success:function(data){alert(data);}     

 });    

};      

</script>




view2.php code




echo $_POST['ad_id'];



Thanks Rick for your reply I have already tried this but it gives me error “Undefined index: ad_id” for $_POST[‘ad_id’]

Can you please explain me how to check ajax url since I am new to ajax.

You can check through firebug [ for firebox browser ) console .