加载或重新加载页面时,我的Activeform会触发

时间:2013-07-03 17:42:04

标签: php drop-down-menu yii form-submit active-form

每当通过重定向/渲染/刷新加载或重新加载页面时,它似乎会自动提交不断提交到数据库的最后信息。我尝试在add方法中添加限制,但它似乎保存了之前提交的信息,允许它通过isset $ _POST。

包含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被触发时触发。

/* 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 个答案:

答案 0 :(得分: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'
)); ?>