Display Record After Add In Db [Ajax]

Hello,

I have ajaxSubmitButton :


 echo CHtml::ajaxSubmitButton(

                'skomentuj', array('task/reqTest03'), array(

            'update' => '#req_res02',

                )

        );

And function:


 public function actionReqTest03() {

        if (isset($_POST['comment'])) {

            $modelMess = new Message;

            $modelMess->created = new CDbExpression('NOW()');

            $modelMess->user_id = Yii::app()->user->getId();

            $modelMess->task_id = $_POST['task_id'];

            $modelMess->text = $_POST['comment'];

            $modelMess->hour = date('H:i:s');

            $modelMess->save();

            echo CHtml::encode(print_r($_POST['comment'], true));

        }

    }

foreach :


<?php foreach ($model->messages as $messages) { ?>

            <br><span style="color:green;font-weight:bold;"><?php echo CHtml::encode($messages->user->name); ?></span>&nbsp;&nbsp

            <span style="color:rgb(58, 98, 124);font-weight:bold;">(<?php echo CHtml::encode ($messages->created); ?>,</span>&nbsp;

            <?php echo CHtml::link('<div id="deletetask" style="margin-left:300px;margin-top:-17px;"></div>', array('task/DeleteMessage', 'id' => $messages->id)); ?>

            <span style="color:rgb(58, 98, 124);font-weight:bold;"><?php echo CHtml::encode ($messages->hour); ?>)</span><br>

            <?php echo $messages->text; ?><br>

            

        <?php } ?>

When i add new message, all save in db. But how i can displaying this message in page, without reloading the side? AJAX METHOD.

I think that you’ll need to handle response ajax

with a js function, passing reference to $ajaxOptions array

of your ajaxSubmitButton. So you can update your span or div

with new message.

you have some example?




<script language="javascript">

	function successASB(data)

	{

		$('#req_res02').html(data);

	}

</script>


<?php

 echo CHtml::ajaxSubmitButton(

                'skomentuj', array('task/reqTest03'), array(

            		'success' => 'successASB',

                )

        );

?>

<div id="req_res02"></div>




Remember to handle update of req_res02 in success function.

If you have to display last post, for example, you need to return

a json array from request to handle data correcly.

Your example doesn’t works, div not reloading.

Have you check problem with firebug (console) ?

maybe div is reloading, but when i add new message, older text hide, and new text show.

example:


user (date)

message message message

 

user2 (date)

message message message 


user3 (date)

message message message 

<div id="req_res02">There is reloading message in textField</div>

but when i write new message, this reloading message delete and put new message. I need displaying that:


user (date)

message message message

 

user2 (date)

message message message 


user3 (date)

message message message


<div id="req_res02">

user4 (date)

message message message 

And when i add new message div add message inside.

Your function return only last record inserted.

So you can try only update div :




<script language="javascript">

        function successASB(data)

        {

                var existingData = $('#req_res02').html();

                $('#req_res02').html(existingData+data);

        }

</script>



Ok, now this add and reloading message, but how i can add text message + user + date inside? or how loading record in datebase last adden?

With this function you are already viewing the list of previous messages with the last inserted

Yes, but, in "req_res02" is only "text" message

In req_res02 you have result of calling action. If you output

also field that you need, you can complete your div.

Thx, i render now all $_POST in my DIV = "req_res02".