2013-07-03 89 views
0

每當通過重定向/渲染/刷新加載或重新加載頁面時,它似乎會自動將最後提供的信息提交到數據庫中。我試着給add方法添加限制,但它似乎保存了以前提交的信息,這允許通過傳遞isset $ _POST。當頁面加載或重新加載時,我的Activeform會觸發

包含actionform的視圖。

<div class="form offset2"> 
<?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
    'id'=>'userTeam-form', 
    'enableAjaxValidation'=>false, 
    // Check thta the action method below is correct 
    'action'=> array('/User/AddTeamMessage', 'id' => $model->id), 
)); ?> 

<!-- 
    Would allow user to access specific team messages and control how much gets display. 
    still under construction. 
--> 
    <div class="row"> 
     <?php 
      echo CHtml::dropDownList("teamId", 'id', Chtml::listData($model->memberOfTeams, 'id', 'teamName'),array(
       'empty'=>'Select Team', 
       'ajax'=>array(
        'type'=>'POST', // request type 
        'url'=>CController::createUrl('DisplayMessage'), 
        'update'=>'#teamMessages', // selector to update 
        'data'=>array('teamId'=>'js:this.value'), 
        ) 
       ) 
      ); 
      echo CHtml::dropDownList("teamMessages", '', array(), array('prompt'=>'Select Messages')); 
     ?> 
    </div> 

<!-- 
    Only works for coaches 
    Allows coaches to submit team messages. 
--> 
<?php if ($model->isCoach()) { ?> 
    <!-- Text area for the coach to enter messages in --> 
    <textarea name="addTeamMessage" class="span5" rows="5" style="resize: none;"></textarea> 
    <!-- submit button --> 
    <?php echo CHtml::submitButton('Submit Message', array(
     'class' => 'btn btn-primary', 
     'name' => 'submitTeamMessage' 
    )); ?> 
<?php } ?> 
<!-- end the widget. everything will be send to UserController/AddTeamMessages --> 
<?php $this->endWidget(); ?> 

當的ActiveForm是surppose爲觸發控制器。

/* add a team message submitted by the coach of the team */ 
public function actionAddTeamMessage($id) 
{ 
    /* check if team and message aren't null */ 
    if(isset($_POST['submitTeamMessage'])) 
    { 
     if(isset($_POST['teamId']['addTeamMessage'])) 
     { 
      try 
      { 
       /* creates a new message */ 
       $teamModel = new TeamMessage; 
       $teamModel->teamId = $_POST['teamId']; 
       $teamModel->content = $_POST['addTeamMessage']; 
       $teamModel->sendTime = new CDbExpression('NOW()'); 
       $teamModel->save(); 
      } 
      catch(Exception $e) 
      { 
       echo "Unable to save."; 
      } 
     } 
    } 
    /* render the profile page for the current user */  
    $user=User::model()->findByPk($id); 
    $this->render('profile', array(
     'model' => $user)); 
} 

回答

1

是不是也發送數據時,你作爲一個教練登錄時轉到頁?

如果不是: 問題很可能是提交按鈕,因爲無法提交活動表單。 將它放在isCoach if語句之外。

<?php if ($model->isCoach()) { ?> 
<!-- Text area for the coach to enter messages in --> 
<textarea name="addTeamMessage" class="span5" rows="5" style="resize: none;"></textarea> 
<?php } ?> 
<!-- submit button --> 
<?php echo CHtml::submitButton('Submit Message', array(
    'class' => 'btn btn-primary', 
    'name' => 'submitTeamMessage' 
)); ?> 
+0

只有當我以教練的身份登錄時,如果非教練員工作得很好......因爲只有教練可以訪問控制器操作以將信息添加到數據庫。 – user2205196

+0

有沒有人知道爲什麼它會一直觸發我的AddTeamMessage,當我刷新頁面? – user2205196

相關問題