在cakephp中选择单选按钮后如何提交值?

时间:2014-06-24 12:25:12

标签: cakephp

我的视图代码在这里......什么是控制器的代码,plz任何人都可以解释。

   <td><?php $attributes = array();
      $options = array($question['Question']['opt1']);
      echo $this->Form->radio($i, $options, $attributes);?>&nbsp;</td>


    <td><?php $attributes = array();
      $options = array($question['Question']['opt2']);
      echo $this->Form->radio($i, $options, $attributes);?>&nbsp;</td>


    <td><?php $attributes = array();
      $options = array($question['Question']['opt3']);
      echo $this->Form->radio($i, $options, $attributes);?>&nbsp;</td>

    <td><?php $attributes = array();
      $options = array($question['Question']['opt4']);
      echo $this->Form->radio($i, $options, $attributes);?>&nbsp;</td>
 </tr>

 <?php endforeach; ?>
<td><?php echo $this->Form->end(__('Submit'),array('controller'=>'question')); ?></td>

3 个答案:

答案 0 :(得分:0)

就这么简单。在您的问题控制器中,我们要说您的表单已提交给&#39; / questions / yourActionMethodName&#39;

您的代码应该类似于:

public function yourActionMethodName(){

    if($this->request->is('post') && !empty($this->request->data)){
        $this->Question->create(); //to save a new record
        $this->Question->save($this->request->data);
    }
}

这应该创建一个新的新记录,设置字段&#39; opt1&#39;的数据库列值。到用户选择的值。我不确定你要解决的是什么情况。这应该可以让您了解保存到数据库的方式。告诉我有关案例的更多信息,我可以编辑代码以满足您的需求。

答案 1 :(得分:0)

您需要输入此模型和名称

   <td><?php $attributes = array();
      $options = array($question['Question']['opt1']);
      echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?>&nbsp;</td>


    <td><?php $attributes = array();
      $options = array($question['Question']['opt2']);
      echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?>&nbsp;</td>


    <td><?php $attributes = array();
      $options = array($question['Question']['opt3']);
      echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?>&nbsp;</td>

    <td><?php $attributes = array();
      $options = array($question['Question']['opt4']);
      echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?>&nbsp;</td>
 </tr>

<?php endforeach; ?>
<td><?php echo $this->Form->end(__('Submit')); ?></td>

在你的控制器中如果需要保存很多字段,你需要使用saveAll或saveMany dep

public function Name(){

    if($this->request->is('post') && !empty($this->request->data)){
        $this->Question->create();
        $this->Question->save($this->request->data);
    }
}

答案 2 :(得分:0)

您必须在控制器中加载模型才能在数据库中创建新记录 你的观点看起来像这样:

<?php echo $this->Form->create('Question');?>
<td><?php $attributes = array();
      $options = array($question['Question']['opt1']);
      echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?>&nbsp;</td>


    <td><?php $attributes = array();
      $options = array($question['Question']['opt2']);
      echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?>&nbsp;</td>


    <td><?php $attributes = array();
      $options = array($question['Question']['opt3']);
      echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?>&nbsp;</td>

    <td><?php $attributes = array();
      $options = array($question['Question']['opt4']);
      echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?>&nbsp;</td>
 </tr>

<?php endforeach; ?>
<td><?php echo $this->Form->end(__('Submit')); ?></td>

现在在您的控制器中,您必须在创建新记录之前加载模型

public function NameOfYourFunction(){

    $this->loadModel('Question');

    if ($this->request->is('post')) {                
            if ($this->Question->save($this->request->data)) {
                  $this->Session->setFlash(__('Information save succesfully.'));
                  return $this->redirect(array('action' => 'index'));
            }
        }

}

我希望我帮助你