非法字符串偏移'名称'

时间:2014-03-29 21:13:57

标签: php jquery yii

我正在使用Yii框架,但如果我没错,我认为它与myjquery相关

我渲染一个帖子列表

<?php
/* @var $this PostController */
/* @var $data Post */
?>

-- Get Post Subject

    <b> <?php echo CHtml::encode(Contest::model()->getAttributeLabel('subject')); ?>:</b>
    <?= CHtml::encode($data['subject']); ?>

-- Place to get the controller/action from js script

    <div id="dataComment<?php echo $data['id']; ?>" data-url="<?php echo Yii::app()->createUrl('post/ajax', array('post_id' => $data['id'])); ?>"></div>

-- Button Link    -- error when I click on the following button

**-- localhost/blog/index.php/post/ajax?post_id=16   -- Illegal string offset 'name'**


    <p><input type ="button" id="comment<?php echo $data['id']; ?>" 
    value="<?php echo ($comment_exist == NULL) ? 'Comment' : 'Cancel my Comment'; ?>"></p>

- Jquery脚本 - 准备好文件

$('[id^="comment"]').click(function(e) {

            var $thisClicked = $(this);
            var parameter = $thisClicked.attr('id');
            parameter = parameter.replace('comment','');

            $.ajax({
                'url' : $('#dataComment' + parameter).data('url'),
                'type' : 'POST',
                'update': '.form', 
            });                    

            $thisClicked.parent().next('.form').fadeIn(1500).slideDown(1300); 

        });

- Post Controller中的Ajax操作

public function actionAjax($post_id){

    $model = new Comment;

    $this->renderPartial('_formComment',array(
            'model'=>$model,
            'post_id'=>$post_id,

        )); 
}

- _formComment.php

<div class="form" style="display:none">

        <?php $form=$this->beginWidget('CActiveForm', array( 
            'id'=>'comment-form', 
            'enableAjaxValidation'=>true, 
            'enableClientValidation'=>true, 
            'focus'=>array($model,'comment'),             
            )); 
        ?> 

        <div class="row">
            <?php echo $form->labelEx($model,'comment'); ?>
            <?php echo $form->textField($model,'comment',array('size'=>60,'maxlength'=>140)); ?>
            <?php echo $form->error($model,'comment'); ?>
        </div>

        <?php echo $form->hiddenField($model,'post_id',$post_id); ?>


        <div class="row buttons">

            <?php
                    echo CHtml::ajaxButton('ajaxButton',

                    array(
                    'type' => 'POST',
                    'success'=>'js:function(string){ alert(string); }'),
                    array('class'=>'someCssClass',));

                    ?>


            <p><input type ="button" id = "cancelComment" value="Cancel"></p>
            <p><input type ="button" id = "deleteComment" value="Delete"></p>
        </div>

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

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

替换隐藏的字段

<?php echo $form->hiddenField($model,'post_id',$post_id); ?>

由此

<?php $form->hiddenField($model,'post_id',$post_id); ?>