1

I succeed to use Ajax with Yii framework. I renderPartial a form from within a list of post.

What I want to do is to prevent refresh when the user click on the ajaxbutton in the form.

In the beginning of the form I used the following code to activate ajax

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

and at the end of the page I simply have the ajaxbutton

<?php    echo CHtml::ajaxSubmitButton('Save'); ?>

in the action controller I have the following

if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
    {
    echo CActiveForm::validate($comment);
        Yii::app()->end();
    }

When I click on the ajax button, it saves the data but refresh the page, so it display the form. What I want is to stay on the page.

Is anyone to help ? Thank you in advance.

klark
  • 484
  • 1
  • 10
  • 27
  • Make sure that your ajax response doesn't include again your JS files like jQuery. You might have created some kind of issue that is described here: http://stackoverflow.com/questions/11078103/prevent-loading-of-jquery-assets-with-ajaxbutton-ajaxsubmitbutton-on-yii-framewo – Pentium10 Feb 02 '14 at 10:40

2 Answers2

2

you can prevent the page from refresh, or ask the user if he's sure he want to leave the page by this code:

window.onbeforeunload = function() {
        return "Dude, are you sure you want to leave? Think of the kittens!";
    }

you can check this question: Prevent any form of page refresh using jQuery/Javascript

Community
  • 1
  • 1
Prog Mania
  • 615
  • 9
  • 14
  • Does it means that if the user answer no , the comment won't be saved? I would like to save it without redirecting. @Prog-Mania – klark Feb 02 '14 at 09:18
  • yes this shall shows a confirm box to the user asking if he's sure to reload the page and you can append a msg saying that there is some work is still processing in the background. @sanfisa – Prog Mania Feb 03 '14 at 06:47
0

I think you have some thing like $this->redirect( ... ));

in your controller after $model->save() , right?

so don't redirect there

Developerium
  • 7,155
  • 5
  • 36
  • 56
  • In the action where I check the ajax call, I finished by returning the $comment variable @tinyByte – klark Feb 02 '14 at 09:15