dongsheng8158 2013-04-30 17:24
浏览 26
已采纳

Yii在“查看”页面中提交表单

I just started working with yii and I stumbled upon a problem. I have a table called "users" and a table called "messages". I basically want to have a page where I can view a user's details but also send him a message that would be saved in the message table.

I have a view called "user/view.php" which consists of:

<h1>View User #<?php echo $model->id; ?></h1>

<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'id',
        'username',
        'first_name',
        'last_name',
    ),
)); ?>

<?php $message=new Message;
echo $this->renderPartial('_messagesend', array('model'=>$message)); ?>

the _messagesend form (created using gii) looks like:

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'message-_messagesend-form',
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    //for the sake of simplicity lets just insert the id's manually for now
    <div class="row">
        <?php echo $form->labelEx($model,'idFrom'); ?>
        <?php echo $form->textField($model,'idFrom'); ?>
        <?php echo $form->error($model,'idFrom'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'idTo'); ?>
        <?php echo $form->textField($model,'idTo'); ?>
        <?php echo $form->error($model,'idTo'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'msg'); ?>
        <?php echo $form->textField($model,'msg'); ?>
        <?php echo $form->error($model,'msg'); ?>
    </div>


    <div class="row buttons">
        <?php echo CHtml::submitButton('Send'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

I have a simple view in my UserController to display the details info:

public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id)
        ));
    }

And now I want to figure out how i can add the controller to save the message. After gii creation I get a code which I tried to use and modify a little bit:

public function actionMessagesend()
    {
        $model=new Message;

        if(isset($_POST['Message']))
        {
            $model->attributes=$_POST['Message'];
            if($model->save()){
                $this->redirect(array('admin'));
                }
        }
        $this->render('_messagesend',array('model'=>$model));
    }

I tried to add this controller function in the UserController.php but it doesn't seem to work, I tried to add the same function to MessageController.php but it also doesn't seem to work. I tried to remove all the code and only add a redirect to show if the controllercode actually hits but it doesn't redirect (i tried it both in usercontroller and messagecontroller). So I have a feeling my code isn't reached. You guys have any idea what I'm missing here?

Maybe a little extra question: Is there a better suggestion to do what I want to do?

Thanks a lot!

Best regards,

WtFudgE

  • 写回答

2条回答 默认 最新

  • douduan1953 2013-04-30 19:42
    关注

    Your message sending action isn't reached because by default the CActiveForm submits to the current page, so you will have to add the message processing to the view action, or you will need to set the action property for your CActiveForm.

    Here is the link to the property documentation: http://www.yiiframework.com/doc/api/1.1/CActiveForm#action-detail

    So the form widget in the view should be something like:

    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'message-_messagesend-form',
        'enableAjaxValidation'=>false,
        'action' => array( '/message/messageSend' ), // change depending on your project
    )); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效