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条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。