LoginSignup
0
1

More than 3 years have passed since last update.

【CakePHP】フォーム内のhiddenに配列を設定し、コントローラーで受け取る方法

Posted at

実現したかったこと

hiddenに配列をもたせて、コントローラー側で取得する。

改善前

cakeのビュー側
<?php echo $this->Form->hidden('hoge',array('value' => 1)); ?>
<?php echo $this->Form->hidden('hoge',array('value' => 2)); ?>
<?php echo $this->Form->hidden('hoge',array('value' => 3)); ?>

クロムの検証ツールで覗いてみる

<input type="hidden" name="hoge" value="1">
<input type="hidden" name="hoge" value="2">
<input type="hidden" name="hoge" value="3">

よし、いい感じ。

コントローラー側で受け取れているか見てみる。

コントローラー側のアクションの中で実行

print_r($this->request->getData("hoge"));
exit;
画面出力結果
3

なんでやねん(笑)

自分が期待してた(欲しかった)結果
Array ( [0] => 1 [1] => 2 [2] => 3 )

修正した箇所

cakeのビュー側
<?php echo $this->Form->hidden('hoge[]',array('value' => 1)); ?>
<?php echo $this->Form->hidden('hoge[]',array('value' => 2)); ?>
<?php echo $this->Form->hidden('hoge[]',array('value' => 3)); ?>

のようにhogeの後ろに[]をつけてあげれば期待通りの配列が取得できました。

終わりに

解決出来てよかったですが、なんで改善前の記述だと「3」が返ってくるのか、いまいちわからないのが現状です。。。

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1