views:

142

answers:

2

Hi, In CakePHP, the value passed as a parameter in the url can be obtained by the controller attribute

            <?php $userid= $this->params['pass'][0];?>

I want to use $userid inside the jQuery code.

 $("#displayPanel #saveForm").live("click", function(){

          document.location = 'http://localhost/cake_1_2/forms/homepage';

 });//Click on SaveForm

Suppose if the userid is 12, I need the document.location to be 'http://localhost/cake_1_2/forms/homepage/12'.

How to use the php variable in jQuery?

+1  A: 

You'll need to output the variable into the JavaScript source:

$("#displayPanel #saveForm").live("click", function(){
          document.location = 'http://localhost/cake_1_2/forms/homepage/&lt;?=$userid?&gt;';
});//Click on SaveForm

Note the <?=$userid?>. If shorthand is turned off on your server, use <?php echo $userid; ?>.

Blixt
where do i add the code <?php $userid= $this->params['pass'][0];?>?
Angeline Aarthi
You would add that code somewhere earlier in your PHP script. Note that I'm assuming the above JavaScript lies in a PHP script already, because PHP is executed on the server and therefore you have to modify the JavaScript in a PHP script.
Blixt
Its actually a .ctp file, I just have the functions written in jQuery and html code for presentation.Is there any way in jquery to get the value passed in the url?
Angeline Aarthi
+2  A: 

For readability, how about:

<?php $userid= $this->params['pass'][0];?>
var userId = '<?=$userid?>';
$("#displayPanel #saveForm").live("click", function(){
    document.location = 'http://localhost/cake_1_2/forms/homepage/' + userId;
});
karim79
where do i add the code <?php $userid= $this->params['pass'][0];?>?
Angeline Aarthi
See my edit. You just need to include somewhere before you use it
karim79
thank you.. I get the answer
Angeline Aarthi