tags:

views:

47

answers:

2

I'm somewhat new to Ajax/Jquery, so I apologize if this is an easy question, but I just couldn't figure it out.

I am using CakePHP and JQuery.

I want to save a link, label, and description into a table by pulling out the "innerHTML" from an HTML page. I can't seem to put the data in the expected format - that is the format that the controller expects.

I am pulling the data from html, the html is like this:

<div class="listing">
 <ul>
   <li class="link">www.yahoo.com</li>
   <li class="label">Yahoo</li>
   <li class="description">This is Yahoo's home page</li>
   </ul>
</div>
...   

I can parse the HTML and get my "link", "label", and "description".

But, when I post the data to the controller, I can't figure out how to get the data in the expected format.

After pulling the data into variables with JavaScript (JQuery), I post it using the following JQuery function:

$.post("/links/save", {link: link, label: label, notes: description});

When the data posts to the controller, the format of the data is:

(
  [form] => Array
  (
    [link] => www.yahoo.com
    [label] => Yahoo
    [description] => This is Yahoo's home page
  )
)

The format that the controller expects the data is:

(
  [data] => Array
  (
    [Link] => Array
    (
      [link] => www.yahoo.com
      [label] => Yahoo
      [description] => This is Yahoo's home page
    )
  )
)

I know I can take the data as it is and put it in the proper format in the controller, but it seems like that is unnecessary.

Can someone please tell me how to manipulate the data in the JQuery so that it posts as the controller expects it?

A: 

Is it $this->params['form'] you're after?

http://book.cakephp.org/view/55/The-Parameters-Attribute-params

Ben
Thanks Ben. The $this->params['form'] could work, although my "save" function mostly uses the $this->data variable. FYI - I have a standard "save" form which allows users to manually input data. I also have this other method of saving data so I don't want to code specifically for this ajax method
+1  A: 

There's still PHP in CakePHP, so you can simply use $_POST.

It may be more convenient to stick to Cake's data structures though, so to get $this->data populated automatically you have to format the data like this:

{ data : { ModelName : { link : ..., ... } } }
deceze
This is more of what I'm after, but it didn't work. When I try this format, I see [object Object] as the posted data.Any suggestions?
@andy Ah, sorry, wasn't sure off the top off my head how jQuery serializes these. Not sure if functions like `$.serializeArray()` would help. Worst case you should be able to construct the right format like this: `{ 'data[Model][link]' : ..., ... }`
deceze
That did it! Thanks!