tags:

views:

352

answers:

2

Hi,

I am using Cakephp and Jquery for my application.

My JQuery code is

                       $(document).ready(function(){

     var str,fields;
         function showValues() {
           str = $("form").serialize();
           $("#results").text(str);
         }
                                                $("input").change(showValues);
         showValues();
                  $(".submit").click(function (){
                   alert(str);
    $.ajax({
      type: "POST",
      url: "http://localhost/cake_1.2.1.8004/index.php/results/submit1",
      data: "str="+str,

      success: function(msg){
       alert( "Data Saved: " + msg);
     }

     });//ajax
                    return false;
            });//submit click



    });//ready

while the alert inside click of submit function displays the entrie thing eg.. _method=POST&name=a

But when i post this value and retriving in the controller its displaying only _method=POST..

My controller code is like,

 function submit1($id = null)
  {


   echo "in ctrller ".$_POST['str'];


  }

How to get the entire value in the controller and to save.. Or is there any method to retrive .. Please suggest me.....

A: 

The data parameter for $.ajax call takes a string in this format:

param1=val1&param2=val2

now, you are already producing a string in this format using the $("form").serialize(); so the correct way to assign it is like this:

  $.ajax({
    type: "POST",
    url: "http://localhost/cake_1.2.1.8004/index.php/results/submit1",
    data: str
  })

Your controller can then inspect the $_POST array for the parameter you are looking for.

Or print the POST'ed params as query string:

echo http_build_query($_POST);
duckyflip
hi but the echo http_build_str($_POST) is not working ...Please suggest me...
Aruna
Sorry the corerct function is http_build_query
duckyflip
A: 

I don't know if it's related to your problem (although knowing Cake, there's a good chance), but that's not the proper way to pass data from a form in Cake. What you should do is use data[key] as the key. Cake will take this and build an array called $this->data from it. If you use the FormHelper to build your HTML form, it will automatically set the name attribute to data[Model][attribute], which you can access using $this->data['Model']['attribute'] or pass it to a saving function, which is the actual intended usage.

macamatic