Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving POST data with CakePHP without formhelper

Tags:

post

php

cakephp

I'm trying to pass a POST variable to one of my controllers, however I'm doing this from a static page (I know, not the cleanest and most efficient way to go about things. But for the sake of learning...). How can I read that POST variable in my controller if the POST data is being sent without a FormHelper form?

I'm posting the data using jQuery ajax, so this is without the CakePHP native "FormHelper".

Does this make sense? Let me know if I need to elaborate. I appreciate any help you can provide :)

like image 689
Baehr Avatar asked Oct 20 '11 03:10

Baehr


2 Answers

You should be able to access the data with:

$this->params['form']['YOUR_VARIABLE_NAME']

And if you follow the naming convention used by the FormHelper and name your input field like data[ModelName][FieldName], then you can access the data as usual with:

$this->data['ModelName']['FieldName']
like image 51
dhofstet Avatar answered Sep 29 '22 21:09

dhofstet


Don't forget Cake is just PHP.

class BazController extends AppController {

    function foo() {
        $foo = $_POST['bar'];
        $this->set('foobar', $foo);
    }

}

is perfectly valid. But I would do as @dhofstet suggests as it's much more "cakey".

like image 30
Ross Avatar answered Sep 29 '22 21:09

Ross