Ajax Refresh Div

Hello,

How i can refresh some div in "_form.php".

I have div:


 <div id="refresh">

            <?php echo $model->foreachMessage(); ?>

        </div>

function "foreachMessage(); inside display record in database.

When i add comment , ajax render $_POST in "refresh" DIV. But i need auto refresh this div delay 2s and function "foreachMessage" he is also being refreshed. How i can do it?

Use setInterval javascript function

simple example :




Yii::app()->clientScript->registerScript('autoUpdateState', "

var currentRequest = null;

function getState() {

        if (currentRequest)

            return;

	currentRequest = $.ajax({

		type: 'GET',

		url: '" . $this->createUrl('stateAjax', array('id' => $id)) . "',

		contentType: 'application/json; charset=utf-8',

		success: updateState,

		error: function(xhr) {}

	}).always(function() {

        currentRequest = null;

        });;

}

function updateState(json) {

		obj = eval('('+json+')')

        var nodeData = document.getElementById('refresh');  

        $(nodeData).empty().append(obj); 

}

$(document).ready(function() {

	setInterval(function() {

                getState();

	}, 2000);

});

");

Please notice that i used currentRequest variable to avoid two active request on the same time.

Your controller action "stateAjax" should return json content of $model->foreachMessage() in this case.