1

When I submit button to update it does not save the data.Here in my view.php file -> `

'id'=>'main-table-grid',
    'dataProvider'=>$dataProvider,
    'columns'=>array(
    array(
      'name'=>'section_no',
      'type'=>'raw',
      'value'=>'CHtml::link($data->section_no)',
    ),
        'section_name',
        'sub_sec_no',
        'sub_sec_name',
        array(
      'name'=>'text',
      'htmlOptions'=>array('width'=>'150'),
      'type'=>'raw',
      'value' => 'CHtml::textArea("MainTable[text]",$data->text)',
    ),
        'review_response',
    array(
      'name'=>'review_comment',
      'htmlOptions'=>array('width'=>'default'),
      'type'=>'raw',
      'value' => 'CHtml::textArea("MainTable[review_comment]",$data->review_comment)',
    ),
    array(
      'class' => 'CButtonColumn',
      'template' => '{update}{view}{delete}',
      'buttons' => array(
        'update' => array(
          'options' => array('class' => 'save-ajax-button'),
          'url' => 'Yii::app()->controller->createUrl("saveModel", array("id"=>$data->id))',
        ),
        'view',
        'delete',
      ),
    ),
    ),
)); 
?>
<script>
   $('#main-table-grid a.save-ajax-button').live('click', function(e)
    {
        var row = $(this).parent().parent();

        var data = $('input', row).serializeObject();

        $.ajax({
            type: 'POST',
            data: data,
            url: jQuery(this).attr('href'),
            success: function(data, textStatus, jqXHR) {
                console.log(text);
                console.log(textStatus);
                console.log(jqXHR);
            },
            error: function(textStatus, errorThrown) {
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
        return false;
    });


    $.fn.serializeObject = function() {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
</script>`

and in my controller I created an action method.The code is below->

public function actionSaveModel($id) {
    $model=$this->loadModel($id);
    $this->performAjaxValidation($model);
    if(isset($_POST['MainTable']))
        {
      $model = new MainTable();
      $model->attributes = $_POST['MainTable'];
      $model->save();
      $this->render('admin', array(
         'model' => $model,
      ));
        }
  }

I have set permission in my controller

    array('allow', // allow authenticated user to perform 'create' and 'update' actions
        'actions'=>array('savemodel'),
        'users'=>array('@'),
    ),

My problem is data is not saving in the table.Please let me know what is the issue here. Thank you.

3
  • I have changed the filters() function added save , create there but the result is always 403 access forbidden.What is the issue there? Commented Jul 17, 2014 at 4:43
  • Ok now getting 500 error.May be it is causing for my function file. Commented Jul 17, 2014 at 5:13
  • can you show how are you storing data before ajax call Commented Jul 17, 2014 at 6:29

2 Answers 2

2

Ok now I have solved the issue.I am getting ajax response by selecting the ID which I need to update and post the data to my controller.

<script type="text/javascript">
$(function() {  
  $('#MainTable_text').live('click', function(e)
    {
        var val=this.value;
        $.ajax({
            type: 'POST',
            data: {des:val},
            url: "<?php echo $this->createUrl("maintable/save"); ?>",
            success: function(data, textStatus, jqXHR) {
                console.log(data);
                console.log(textStatus);
                console.log(jqXHR);
            },
            error: function(textStatus, errorThrown) {
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
        return false;
    });
});

after that I catch the value in controller and update in database.

  public function actionSave() {
    $command = Yii::app()->db->createCommand();
    $user = $_POST['des'];
    if (isset($user)) {
      $command->update('main_table', array(
          'text' => $user,
              ), 'id=:id', array(':id' => $id));
      $this->render('save', array(
          'model' => $model,
      ));
    } else {
      echo 'error found';
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

1

Please put your jQuery code inside this code:

<script type="text/javascript">
$(function() {
// put your jQuery code here
});
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.