Ajax button, strange behavior

hey guys

I got AJax working to delete some files off my server.

basically the main view shows all the files in a particular directory , each file has an AJAX button associated with it.

If I hit refresh and go into the page clean and hit any of the delete links it works perfectly…sends me the prompt to confirm and then deletes the files and updates my view with AJAx… now, if I try and delete a second file (without refreshing) I notice that when the confirm button comes up it’s asking me if I want to delete the file I previously deleted then it asks me about the one I’m trying to delete now… if I go again it will now ask me 3 times,

it’s like has memory of the old files even if they are no longer on the server… .problem goes away if I refresh.

so imagine I have these 5 files

[indent]img1.jpg

img2.jpg

img3.jpg

img4.jpg

img5.jpg[/indent]

when I run my code I get something like




img1.jpg delete

img2.jpg delete

img3.jpg delete

img4.jpg delete

img5.jpg delete



I hit delete on img1.jpg, I get a confirm box asking me

[indent]are you sure you want to delete img1.jpg[/indent]

I say OK and the file gets deleted and my view refreshed via AJAX

now if I hit the delete on the next file (img3.jpg) I get a confirm box asking me:

[indent]are you sure you want to delete img2.jpg[/indent]

If I hit OK it will actually delete the file (img2) and then the prompt remains and asks me

[indent]are you sure you want to delete img3.jpg[/indent]

I hit OK and then the confirm box closes and the file is deleted.

If I try for the third file I’ll get three questions before the box closes , so on and so on

it will ask for as many times as I’ve pressed the delete button without a page refresh.

I noticed it too in Firebug…I will get a


500 PHP Error

for every file I’ve deleted until it finds the current file being deleted.

this is my controller




class FileController extends CController

{

    public function actionIndex()

    {

        $data = array();

        $data["myValue"] = $this->showF();

        $this->render('index', $data);

    }


    public function actionUpdateAjax()

    {

		unlink($_REQUEST["file"]);

        $data = array();

        $data["myValue"] = $this->showF();

        $this->renderPartial('_ajaxContent', $data, false, true);

    }


	public function showF(){

		$msg = null;

		for($j=1; $j < 8; $j++){

			$files = CFileHelper::findFiles('docs/results/day'.$j.'/',  array('fileTypes' => array('JPG','jpg'))) ;

			$msg .= "<h3>".Yii::t('general',"day".$j)."</h3>";

			$i=1;

			foreach($files as $key => $value){

				$msg .= 

				"<div class='docs'>".$i.") ".CHtml::Link(basename($value),$value, array('target'=>'_blank'))." ".

					CHtml::ajaxLink('delete ',Yii::app()->createUrl('file/UpdateAjax',array('file'=>$value)),array('update' => '#data'),array('confirm' => 'Are you sure you want to delete '.basename($value).'?')). 

				"</div>";

				$msg .= $value;

			}

		}

		return ($msg);

	}

}



the content of _ajaxContent.php


<?php echo $myValue ?>

and my index view file




<div id="data">

   <?php $this->renderPartial('_ajaxContent', array('myValue'=>$myValue)); ?>

</div>



any clues?

found it!

simply had to add a unique ID to my AJAX links

the new call to CHtml::ajaxLink looks like this




 CHtml::ajaxLink('delete ',

   Yii::app()->createUrl('helloWorld/UpdateAjax',

                          array('file'=>$value)),

                          array('update' => '#data'),

                          array('confirm' => 'Are you sure you want to delete '.basename($value).'?',

                          'id'=>uniqid())). // The uniqid() is what needed to be added!! 



found it on a wiki here