Use of ajax or something else

this is my view


<div class="divaddnote">

    <div class="divabouthead">

        <span class="headingtext">Add Note</span>

    </div>

    <div class="divnoteinner">

        <?php foreach ($data as $row): ?>

             <div class="divdel">

                <?php

                     echo CHtml::link('Delete',array('/blogger/delnote','param1'=>$row['id']));

                ?>

            </div>

            <div class="divnote">

                <?php echo $row['note']; ?><br>

            </div>     

            <div class="divdate">

                <?php echo $row['date']; ?>

                <?php echo $row['time']; ?><hr>

            </div>

       <?php endforeach; ?>

   </div>

   <div class="divadd">

        <?php $form=$this->beginWidget('CActiveForm', array(

            'id'=>'note-form',

            'enableAjaxValidation'=>True,

        )); ?>

        <div class="row">

            <?php echo $form->textarea($model10,'note',array('size'=>60,'maxlength'=>600)); ?>

            <?php echo $form->error($model10,'note'); ?>

        </div>

        <div class="row buttons">

            <?php echo CHtml::submitButton($model10->isNewRecord ? 'Add Note' : 'Save'); ?>

        </div>

        <?php $this->endWidget(); ?>

    </div>

</div>

this view is for add n delete note n showing the note on same page

but in controller i redirect to home page after deletion and update.

BUt problem is i want that when i delete or add note it will not redirect on different page it should redirect on same page after deletion or add.

i know that i can use ajax.

but i dont know how to use it n where to use it .

Plzz help me

in controller action (where U say U are redirecting) add condition

if(Yii::app()->request->isAjaxRequest)

{

echo CJSON::encode($someResult);

} else {

do redirect

}

i did not understand it sir.

Let me try to explain:

for delete U have this code:

<?php echo CHtml::link(‘Delete’,array(’/blogger/delnote’,‘param1’=>$row[‘id’]));?>

which will generate normal link and lead to blogger/delnote.

What U should do is to intercept this before page loads, collect all data that U need, create ajax request and send it to server, wait for reasponse and remove that row from view page.

Something like:

instead of ‘<div class=“divdel”>’ place

'<div class=“divdel” id="<?php echo $row[‘id’];?>"> // will help U remove this part latter

instead of

<?php echo CHtml::link(‘Delete’,array(’/blogger/delnote’,‘param1’=>$row[‘id’]));?>

place

<?php echo CHtml::link(‘Delete’,array(’/blogger/delnote’,‘param1’=>$row[‘id’],

[‘data-pk’=>$row[‘id’], ‘class’=>‘js-delete’]));?> // add data-pk to use for ajax call and add class to bind event on

and add some js:

$(’.js-delete’).on(‘click’, function(e) {

 e.preventDefault();


 var pk = &#036;(this).data('pk');


 var url = &#036;(this).attr('href');


 &#036;.ajax({


     url : url,


     type : 'post'


     data : {'pk':pk},


     dataType : 'json'


 }).done(function(r) {


    if(r.success)


      &#036;('#'+pk).remove(); // removing parent div if everything is ok


 }).fail(function(r) {


      alert(r.message) // alerting about errors


 })

})

and in controllers action add condition

if(&#036;model-&gt;delete)


{


     if (Yii::app()-&gt;request-&gt;isAjaxRequest)


     {


         echo CJSON::encode([


              'success' =&gt; true,


         ]);





     }else


       // do your redirect


} else {


     


     if (Yii::app()-&gt;request-&gt;isAjaxRequest)


     {


         echo CJSON::encode([


              'success' =&gt; false,


              'message' =&gt; 'some message (maybe &#036;model-&gt;errors)'


         ]);





     }else


       // do your redirect

}

hope that his will lhelp

1 Like