2013-11-28 60 views
0

我想有一個下拉列表和一個提交按鈕,當按鈕按下時,我想獲取所選項目的文本並將其存儲到數據庫中。我認爲下拉代碼是:下拉列表中提交yii

<?php 
           $form=$this->beginWidget('CActiveForm', array(
           'enableAjaxValidation'=>true, 
           'clientOptions'=>array('validateOnSubmit'=>true 
           ), 
           )); ?> 


           <?php echo $form->labelEx($model,'actTopic'); ?> 
           <?php echo $form->error($model,'actTopic'); ?> 
           <?php 
           echo CHtml::dropDownList('actTopic', $category,$list, 
           array('empty' => '... ')); 
           ?> 

它生成一個下拉列表罰款。 但我不知道如何在我的動作函數中獲取選定的項目。 的動作功能是:

public function actiongivePoint() 
{  
    $model=new GivePointForm; 


    if(isset($_POST['GivePointForm'])){ 

     $model->attributes=$_POST['GivePointForm']; 
     if($model->validate()){ 
      $actTopic=($model->actTopic); 
      $actPoint=($model->actPoint); 
      $actId=($model->actId); 
      $stCode = (int) $_GET['stCode']; 
      $connection=Yii::app()->db; 
      $connection->active=TRUE; 

      $actIdReader =Yii::app()->db->createCommand() 
      ->select ('actID') 
      ->from('refrencepoint') 
      ->where("actTopic=:actTopic") 
      ->queryScalar(array(':actTopic'=>$actTopic)); 

      $search =Yii::app()->db->createCommand() 
      ->select ('count(*)') 
      ->from('point') 
      ->where(array('and', 'actId=:actID', 'stCode=:stCode') ) 
      ->queryScalar(array(':actId'=>$actId,':stCode'=>$stCode));   
      if($search !=0){ 
       $month=CDateFormatter::formatMonth(now()); 
       $year=CDateFormatter::formatMonth(now()); 
       $command =$command->update('point', array(
       'year'=>$year,'month'=>$month, 
       'pcounter'=>new CDbExpression('pcounter + 1'),), 
       (array('and','actId'=>$actId, 'stCode'=>$stCode))) ; 

       Yii::app()->user->setFlash('point','....');   
       } 
      else{ 
       $month=CDateFormatter::formatMonth(now()); 
       $year=CDateFormatter::formatMonth(now()); 

       $command->insert('point', array('actId'=>$actId, 
       'stCode'=>$stCode,'year'=>$year,'month'=>$month, 
       'pcounter'=>new CDbExpression('pcounter + 1'))); 

       Yii::app()->user->setFlash('point','....');   
    } 

} 
}       
    $this->render('givePoint',array('model'=>$model)); 

} 

我應該說我不使用活動記錄。

上面的代碼看起來好像一切正​​常,但當我按提交按鈕什麼都沒有發生,沒有數據庫的變化,甚至沒有錯誤,只是頁面刷新!

這裏有什麼問題? 我很累,試圖....

回答

0

我想你只是喜歡它。

您首先使用foreach循環。我不確定您列出的是哪種類型的東西。但你需要像下面這樣工作。

這是一個例子

foreach($model->category as $list) { 
    $list['id'] = $list->name; 
} 
echo CHtml::dropDownList($model, 'actTopic', $list); 

現在您調試代碼。

public function actiongivePoint() 
{  
$model=new GivePointForm; 
if(isset($_POST['GivePointForm'])){ 

print_r($_POST['GivePointForm']); 
exit; //Just check either the form is posted or not and values are coming or not. 

$model->attributes=$_POST['GivePointForm']; 
if($model->validate()){ 
$actTopic=($model->actTopic); 

echo $actTopic ; 
exit ;     // Check either your dropdown item id is coming or not. 

希望它能幫助你。

謝謝。