[Solved] My Ajaxrequest Is Posted Many Time

Hi dear Yiiers,

In my Page admin view, I can explore the Page model tree and open a form for editing a page while selecting an item in the tree.(I’m using simpleTree extension). This work nicely.

But when I update a page with an ajaxSubmit button, the POST request is executed many times, see logs on the screen capture

3552

Capture-1.png

The view:


<div class="row">

	<div class="span3" id="pageTree">

	<?php

	$this->renderPartial('_pageTree',array(),false,true);

	?>

	</div>

	<div class="span9" id="pageEdit">

	</div>

</div>

the pageTree partial:


	<?php

	$this->widget('application.extensions.SimpleTreeWidget',array(

		'model'=>'Page',

		'modelPropertyParentId' => 'parent_id',

		'modelPropertyName' => 'title',

		'ajaxUrl' => $this->createUrl('/content/page/simpletree'),

		'onSelect'=>'

		    var id = data.inst.get_selected().attr("id").replace("node_","");

		    $("#pageEdit").load("edit/id/"+id);

		'

	));

	?>



Page Controller actions:




    //this is for displaying the form

	public function actionEdit($id)

	{

		$model=$this->loadModel($id);

		$this->renderPartial('_update',array('model'=>$model),false,true);

	}


    //for updating

	public function actionUpdate()

	{

		if(Yii::App()->request->isAjaxRequest){

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

				$model=$this->loadModel($_POST['Page']['id']);

				$model->attributes=$_POST['Page'];

				if($model->save()){

					$this->renderPartial('_pageTree',array(),false,true);

				}

			}

		}

	}



and in the form (_update.php):




			<?php $this->widget('bootstrap.widgets.TbButton', array(

				'buttonType'=>'ajaxSubmit', 

				'type'=>'primary', 

				'label'=>'Submit',

				'url'=>$this->createUrl('/content/page/update'),

				'ajaxOptions' => array ('update'=>'#pageTree')

			)); ?>

What’s wrong with this approach ?

Also, same js and css files are loaded many times …

Lets say that it’s the first load of my page admin view: jquery.js , jquery.jstree.js, redactor.min.js are loaded many times.

Why? I do not know.

Can this explain that once I submit the form, the POST action is repeated many times ?

OK, partially solved using this trick: That guy, Alain, has posted only once in the forums. But what a good one.

With that solution, my POST request is sended only once.