0

每当页面通过重定向/渲染/刷新加载或重新加载时,它似乎会自动提交最后提交给数据库的信息。我尝试向 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(); ?>

当活动表单被触发时的控制器。

/* 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));
}
4

1 回答 1

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'
)); ?>
于 2013-07-03T19:04:23.433 回答