CJuiDialog gets saved twice
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
- "{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;
- }
"{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
- $('#dialogClassroom div.divForForm').html(data.div);
- // Here is the trick: on submit-> once again this function!
- $('#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)
- textField($model,'hizoClick', array('class'=>'textField')); ?>
- '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
- if($model->hizoClick==='submit' && $model->save())
- {
- $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)
- 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
- beginWidget('CActiveForm', array(
- ...
- 'enableAjaxValidation'=>false,
- ...
beginWidget('CActiveForm', array(
...
'enableAjaxValidation'=>false,
...Any ideas/Code?
Possibly related Questions
-
Ajax Cform validation within renderPartial view
February 7, 2012 By TheDeveloper 4 Answers
-
CJuiDialog: referencing attribute
January 23, 2012 By msimaremare 2 Answers
TAGS
actioncreate ( x 2 )ACTIVERECORD ( x 4 )AJAX ( x 10 )ajaxSubmitButton ( x 4 )AR ( x 2 )cactivedataprovider ( x 2 )CActiveForm ( x 2 )cactiverecord ( x 2 )CGridView ( x 14 )CJuiAutoComplete ( x 3 )CJuiDialog ( x 8 )CListView ( x 8 )controller ( x 7 )Css ( x 2 )CStarRating ( x 2 )CTreeView ( x 2 )database ( x 5 )dialog ( x 2 )dropdownlist ( x 3 )EGMaps ( x 3 )extension ( x 3 )file ( x 2 )form ( x 6 )Giix ( x 2 )gridview ( x 2 )html ( x 2 )javascript ( x 3 )many_many ( x 4 )merge ( x 2 )model ( x 4 )Modules ( x 3 )multilanguage ( x 2 )php ( x 2 )redirect ( x 4 )Registration ( x 3 )relations ( x 5 )renderPartial ( x 8 )session ( x 2 )sorting ( x 3 )sql ( x 4 )url ( x 3 )urlManager ( x 3 )validation ( x 3 )yii ( x 11 )YiiAnswers Site ( x 4 )This website counts Questions and 429 Answers by 8901 registered users.

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