Lars C. Magnusson
Lars C. Magnusson

Reputation: 349

jQuery POST to CakePHP $this->data

I'm trying to use jQuerys post-function to post a form to a CakePHP-script.

Like this:

jQuery:

$('#submit_btn').click(function(){

   //Code to prevent redirect

   dataString = 'test=testdata';

   $.post('cakephp/forms/output', dataString, function(response){
      alert(response);
   })
});

CakePHP

function output(){
   pr($this->data);                # Print nothing
   pr($_POST);                     # Print test => testdata 
   $this->render('view','ajax');   # Render ajax-friendly
}

So $_POST isn't empty but $this->data is... how come??

The form element i which to post data from is got from aja, if that is something that matters in this case.

Upvotes: 3

Views: 6218

Answers (2)

Belinda
Belinda

Reputation: 1231

$this->data expects your variable names to be in the form

data[Model][Property]

For your example change dataString to data['ModelName']['test']=test data

and it should work.

Upvotes: 5

Wolfram
Wolfram

Reputation: 8052

It could be that the controller's data attribute is only prefilled when the POST data conforms to an existing model attribute, like when data is POSTed via the form helper. So you might have to send "Test.something=testdata", so that you can access $this->data["Test"]["something"] in the controller.

Upvotes: 0

Related Questions