klark
klark

Reputation: 494

ajaxbutton how to prevent refresh of the page

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.

Upvotes: 1

Views: 757

Answers (2)

Developerium
Developerium

Reputation: 7265

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

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

so don't redirect there

Upvotes: 0

Prog Mania
Prog Mania

Reputation: 624

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

Upvotes: 2

Related Questions