Asim Zaidi
Asim Zaidi

Reputation: 28284

cakephp and jquery submission data post

I am using jquery with cakephp and I need to post some data to a update function of controller. I need it to be separated so that it would look like this in fiddler

Name                   |    Value
data[Answer][1][body]  |    John DC
data[Answer][2][body]  |    Company
data[Answer][3][body]  |    Title
data[Answer][4][body]  |    Country
data[Answer][5][body]  |    Email
data[Answer][6][body]  |    Phone
data[Answer][7][body]  |    test




Name                                                                                                                   |  Value
    data[Answer][1][body]:John DC,data[Answer][2][body]:Company,data[Answer][3][body]:Title,data[Answer][4][body]:Country,data[Answer][5][body]:Email,data[Answer][6][body]:Phone,data[Answer][7][body]:test    |

So it all shows up under Name column.

Here is my ajax

$j(document).ready(function() { 
    $j('#form').click( function () {
        alert("hi");
        $j.ajax({

             type: 'post',    

             data:  
                    "data[Answer][1][body]:" +  $j('#ID1').val() + 
                    ",data[Answer][2][body]:" + $j('#ID2').val() + 
                    ",data[Answer][3][body]:" + $j('#ID3').val() + 
                    ",data[Answer][4][body]:" + $j('#ID4').val() +
                    ",data[Answer][5][body]:" + $j('#ID5').val() +
                    ",data[Answer][6][body]:" + $j('#ID6').val() +
                    ",data[Answer][7][body]:" + $j('#ID7').val(),

             url: "/mypage/update",
             success: function(){
                alert("Done");
              }
            });

     });  
 });

thanks

Upvotes: 0

Views: 536

Answers (1)

contrebis
contrebis

Reputation: 1318

Is there a good reason your elements #ID1 to #ID7 don't have the name attribute in the format you need?

This will happen automatically if you use echo $this->Form->input('Answer.1.body') to render your input fields. If you don't like what FormHelper gives you by default there are many ways to customize the output.

You can then use something like jQuery("#ID1,#ID2...").serialize() to help post your data back to the server. Seems like you need to let both the jQuery and CakePHP frameworks do more of the work for you :)

Upvotes: 1

Related Questions