Yii Framework Forum: How To Use Post Or Get To Pass Variable From One View To Other Viiew - Yii Framework Forum

Jump to content

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

How To Use Post Or Get To Pass Variable From One View To Other Viiew Rate Topic: -----

#1 User is offline   prat 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 124
  • Joined: 13-September 13

Posted 23 November 2013 - 05:58 AM

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?
0

#2 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 25-June 13

Posted 25 November 2013 - 04:03 AM

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

View Postprat, on 23 November 2013 - 05:58 AM, said:

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?

Thanks & Regards,
S.A. Johnson
www.arojohnson.tk
0

#3 User is offline   prat 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 124
  • Joined: 13-September 13

Posted 25 November 2013 - 04:37 AM

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.
0

#4 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 25-June 13

Posted 25 November 2013 - 04:54 AM

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!


View Postprat, on 25 November 2013 - 04:37 AM, said:

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.




Thanks & Regards,
S.A. Johnson
www.arojohnson.tk
0

#5 User is offline   Rick's 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 18-October 13

Posted 25 November 2013 - 11:04 AM

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.
0

#6 User is offline   Bob G 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 13-July 12

Posted 25 November 2013 - 01:57 PM

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"
0

#7 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 25-June 13

Posted 25 November 2013 - 11:31 PM

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.

View PostRick, on 25 November 2013 - 11:04 AM, said:

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.




Thanks & Regards,
S.A. Johnson
www.arojohnson.tk
0

#8 User is offline   prat 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 124
  • Joined: 13-September 13

Posted 26 November 2013 - 01:55 AM

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

#9 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 25-June 13

Posted 26 November 2013 - 02:16 AM

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!

View Postprat, on 26 November 2013 - 01:55 AM, said:

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




Thanks & Regards,
S.A. Johnson
www.arojohnson.tk
1

#10 User is offline   prat 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 124
  • Joined: 13-September 13

Posted 26 November 2013 - 03:22 AM

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.
0

#11 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 25-June 13

Posted 26 November 2013 - 08:28 AM

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;


View Postprat, on 26 November 2013 - 03:22 AM, said:

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.




Thanks & Regards,
S.A. Johnson
www.arojohnson.tk
0

#12 User is offline   prat 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 124
  • Joined: 13-September 13

Posted 27 November 2013 - 01:55 AM

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?
0

#13 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 25-June 13

Posted 27 November 2013 - 04:56 AM

Please checkout your ajax url


View Postprat, on 27 November 2013 - 01:55 AM, said:

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?




Thanks & Regards,
S.A. Johnson
www.arojohnson.tk
0

#14 User is offline   Rick's 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 18-October 13

Posted 27 November 2013 - 09:20 AM

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'];

0

#15 User is offline   prat 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 124
  • Joined: 13-September 13

Posted 28 November 2013 - 12:48 AM

Thanks Rick for your reply I have already tried this but it gives me error "Undefined index: ad_id" for $_POST['ad_id']
0

#16 User is offline   prat 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 124
  • Joined: 13-September 13

Posted 28 November 2013 - 12:49 AM

View PostArockia Johnson, on 27 November 2013 - 04:56 AM, said:

Please checkout your ajax url

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

#17 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 25-June 13

Posted 28 November 2013 - 01:21 AM

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

View Postprat, on 28 November 2013 - 12:49 AM, said:

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




Thanks & Regards,
S.A. Johnson
www.arojohnson.tk
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