CakePHP: Send current model ID to another controller via AJAX

Go To StackoverFlow.com

0

So I'm in my edit view... let's says it's posts/edit/1

That page loads some Javascript which makes a $.get call that pulls data from a different action on the same controller (posts/foo).

How can I simply send the current model's ID to AJAX, and on to the other controller?

In this case, both actions are in the same controller, but I'm not sure that helps in this case.

2012-04-04 02:05
by Benjamin Allison


0

if you are using jquery you can have something like this

$(document).ready(function(){
  //ajax call here
});

The thing is that once your document has loaded you just send this id to the other controller.

I hope that helps

2012-04-04 02:11
by Marius Talagiu
I've got all my ajax working swimingly, the problem is figuring out how to cleanly send data from the current model into my jQuery. I'd rather not add a bunch of "script" tags to my view or anything like that - Benjamin Allison 2012-04-04 02:15
you can not really make the model "talk" with jquery because the server side (model) is not able to talk directly with the client side (view) so you need to have some sort of script tag in your view in order for this to work. If you are wondering about validation, $this->Htmk->scriptBlock('script here', array('inline'=>false)); this will output the script block into your header so it will validat - Marius Talagiu 2012-04-04 02:19
Makes sense. This is what I'm doing in my JS. It spits out the ID based on the route, since Cake uses ID's in routing by default. var pathname = window.location.pathname; pathname = pathname.split("/"); var pID = pathname.pop() - Benjamin Allison 2012-04-04 02:54
I think the following is a much more elegant solution and it will make it more with the standards of cakephp. Since you have the hidden input field for the id (since you are on edit) give it and id like $this->Form->input('id', array('id'=>'myhiddenid')); and for your javascript you will have $this->Html->scriptBlock('$(document).ready(function(){ $.ajax({data:$("$("#myhiddenid").val();});});', array('inline'=>false); This is untested code. I hope it help - Marius Talagiu 2012-04-04 16:59


0

If you only need to get the Post ID from your edit view, this is the variable you need:

$this->Form->fields['Post.id']

If its not "Post", just replace it with the model name you are using and trying to get the ID for.

UPDATED: Based on the clarification: you are right; for what you are trying to do, you need to generate the link or just the unique ID in a script tag, and then somehow use that variable in your included script. Take a look at how I did it for a CakePHP plugin. A quick summary:

  1. Include the JS files as you would normally do
  2. Create a variable in a script tag in the controller
  3. Use that variable in the included JS files

Something like this:

<script type="text/javascript">
id = '<?php echo $this->Form->fields['Post.id'] ?>;
</script>

and then use this id variable in your included Javascript file.

2012-04-09 21:45
by Suman
Maybe I should edit the initial question to be more clear. The problem isn't getting the ID field, it's finding the best way for the PHP to tell my JS what it is. It seems my only real options are echoing it in a