Register here!

Forgot password?

ASK NEW QUESTION

Home Page » Zii » Jui » CJuiDialog gets saved twice
0 Votes
Vote Con!
Solved!

CJuiDialog gets saved twice

ajaxSubmitButtonCJuiDialog

Hello all,

Almost there …

I got the CJuiDialog dialog form to work, but I now have a new problem.

Whenever I click to save (it saves twice).

View

  1.                 "{addClassroom();  $('#dialogClassroom').dialog('open'); }", 'class'=>'cta_button agregar'));
  2.                 ?>
  3.  
  4. beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
  5.     'id'=>'dialogClassroom',
  6.     'options'=>array(
  7.         'title'=>'Create classroom',
  8.         'autoOpen'=>false,
  9.         'modal'=>true,
  10.         'width'=>550,
  11.         'height'=>470,
  12.     ),
  13. ));?>
  14.  
  15.  
  16. endWidget();?>
  17.  
  18.  
  19. // here is the magic
  20. function addClassroom()
  21. {
  22.     array('clase/create'),
  23.             'data'=> "js:$(this).serialize()",
  24.             'type'=>'post',
  25.             'dataType'=>'json',
  26.             'success'=>"function(data)
  27.            {
  28.                if (data.status == 'failure')
  29.                {
  30.                    $('#dialogClassroom div.divForForm').html(data.div);
  31.                          // Here is the trick: on submit-> once again this function!
  32.                    $('#dialogClassroom div.divForForm form').submit(addClassroom);
  33.                }
  34.                else
  35.                {
  36.                    $('#dialogClassroom div.divForForm').html(data.div);
  37.                    setTimeout(\"$('#dialogClassroom').dialog('close') \",1000);
  38.                    $.fn.yiiListView.update('ajaxListView') ;
  39.            
  40.                }
  41.  
  42.            } ",
  43.             ))?>;
  44.     return false;
  45.  
  46. }
  47.  
				"{addClassroom();  $('#dialogClassroom').dialog('open'); }", 'class'=>'cta_button agregar'));
				?> 

beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogClassroom',
    'options'=>array(
        'title'=>'Create classroom',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>550,
        'height'=>470,
    ),
));?>

 
endWidget();?>
 

// here is the magic
function addClassroom()
{
    array('clase/create'),
            'data'=> "js:$(this).serialize()",
            'type'=>'post',
            'dataType'=>'json',
            'success'=>"function(data)
            {
                if (data.status == 'failure')
                {
                    $('#dialogClassroom div.divForForm').html(data.div);
                          // Here is the trick: on submit-> once again this function!
                    $('#dialogClassroom div.divForForm form').submit(addClassroom);
                }
                else
                {
                    $('#dialogClassroom div.divForForm').html(data.div);
                    setTimeout(\"$('#dialogClassroom').dialog('close') \",1000);
                    $.fn.yiiListView.update('ajaxListView') ; 
		    
                }
 
            } ",
            ))?>;
    return false; 
 
}
 

I think, the real important thing is

  1.                     $('#dialogClassroom div.divForForm').html(data.div);
  2.                           // Here is the trick: on submit-> once again this function!
  3.                     $('#dialogClassroom div.divForForm form').submit(addClassroom);
                    $('#dialogClassroom div.divForForm').html(data.div);
                          // Here is the trick: on submit-> once again this function!
                    $('#dialogClassroom div.divForForm form').submit(addClassroom);

which some how submits twice the form and gets recorded in the db.
If I comment the second line it only submits it once but then the whole scripts breaks (goes to a whole different page, doesn’t show message that it has been saved correctly.)

But it saves it only once.

But this line is necessary according to this

What I tried adding was a hidden field in the form (view)

  1.     textField($model,'hizoClick', array('class'=>'textField')); ?>
  2.    
  3.         'cta_button agregar', 'onclick'=>'$("#Clase_hizoClick").val( "submit" ); ')); ?>
	textField($model,'hizoClick', array('class'=>'textField')); ?>
	
		'cta_button agregar', 'onclick'=>'$("#Clase_hizoClick").val( "submit" ); ')); ?>

And tried in the controller

  1.             if($model->hizoClick==='submit' && $model->save())
  2.             {
  3.                 $model->hizoClick = "vacio" ;
			if($model->hizoClick==='submit' && $model->save())
			{
				$model->hizoClick = "vacio" ;

Which does not work.
Still gets recorded twice.

I already added in the _form (& just in case in the controller)

  1. Yii::app()->clientscript->scriptMap['jquery.js'] = false;
Yii::app()->clientscript->scriptMap['jquery.js'] = false;

One last idea, if I disable AjaxValidation, it all works but w/o Ajax Validation

  1. beginWidget('CActiveForm', array(
  2. ...
  3. 'enableAjaxValidation'=>false,
  4. ...
beginWidget('CActiveForm', array(
...
'enableAjaxValidation'=>false,
...

Any ideas/Code?

By tuolden in Jui · Asked 597 days 36 mins ago

Questions: 25 Accepted: 0 ( 0% ) | Reputation: 0

 

Answers (1)

  1. 0 votes
    Accepted

    The problem is that by applying the submit javascript event to the loaded data via AJAX you do a double binding. I think it is better than you write the button on the data returned as a normal submit and then use on the form the ‘onsubmit=”addClassroom()”‘ event on the tag itself.

    By tonydspaniard · 596 days 13 hours 40 mins ago

    Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0

© YiiAnswers.com 2011. All rights reserved.

Switch to our mobile site