Tom
Tom

Reputation: 3

CakePHP : Read values of a radio form

I'd like to read the values of a form I'm trying to use :

<!-- File: src/Template/Users/edit.ctp -->
<?php
    echo '<p>Question 1 text</p>';
    echo $this->Form->radio(
    'answer_1',
    [
    ['value' => 'true', 'text' => 'Radio 1 text'],
    ['value' => 'false', 'text' => 'Radio 2 text'],
    ['value' => 'false', 'text' => 'Radio 3 text'],
    ['value' => 'false', 'text' => 'Radio 4 text'],
    ]
    );

    echo '<p>Question 2 text</p>';
    echo $this->Form->radio(
    'answer_2',[
    ['value' => 'false', 'text' => 'Radio 1 text'],
    ['value' => 'false', 'text' => 'Radio 2 text'],
    ['value' => 'true', 'text' => 'Radio 3 text'],
    ['value' => 'false', 'text' => 'Radio 4 text'],
    ]
    );

    echo '<p>Question 3 text</p>';
    echo $this->Form->radio(
    'answer_3',[
    ['value' => 'false', 'text' => 'Radio 1 text'],
    ['value' => 'false', 'text' => 'Radio 2 text'],
    ['value' => 'false', 'text' => 'Radio 3 text'],
    ['value' => 'true', 'text' => 'Radio 4 text'],
    ]
    );

echo $this->Form->button(__("Save"));
echo $this->Form->end();
?>

I would like in my edit function to read the values of the submitted radios, in order to calculate a score for each "true". Do you know how to do it in my controller ? Neither the questions nor the answers are stored in DB, they're hard-coded in the view.

Bonus point : I'd like to save a specific answer of the form in DB, in that possible ? For example, a bonus question :

echo '<p>Bonus question text</p>';
echo $this->Form->control('bonus_answer');

Thanks

Upvotes: 0

Views: 269

Answers (1)

user5236938
user5236938

Reputation:

You can find how to get request data here

3.0 - 3.3

What you're looking for is

$this->request->data('answer_1');
$this->request->data('answer_2');
$this->request->data('answer_3');

AS OF 3.4

The use of $this->request->data() has been deprecated.

What you're looking for is

$this->request->getData('answer_1');
$this->request->getData('answer_2');
$this->request->getData('answer_3');

The "name" of the inputs is the first param of the function, $this->Form->control('INPUTNAME');

Upvotes: 2

Related Questions