I have a 2 model create form. Participants can have up to 6 percentages and the percentages need to add up to 100.
This works if all data are entered correctly. If the percentages don’t add up it gives you a warning and stays on the page to let you fix it. But if you fix it and hit save again, it does the save but doesn’t go to the next screen.
Also if there is an error in validation data.msg and data.result are never defined. I assume this is not understanding javascript more than not understanding yii, but can some one offer a fix?
Thanks
_form.php
<?php echo CHtml::ajaxSubmitButton('Save', CHtml::normalizeUrl(array('participant/create')),
array(
'dataType' => 'json',
'data'=>'js:$("#participant-form").serialize()',
'type' => 'post',
'beforeSend' => "js:function(data){
var result=0;
var p1 = document.getElementById('Percentage_1_percentage').value;
var p2 = document.getElementById('Percentage_2_percentage').value;
var p3 = document.getElementById('Percentage_3_percentage').value;
var p4 = document.getElementById('Percentage_4_percentage').value;
var p5 = document.getElementById('Percentage_5_percentage').value;
var p6 = document.getElementById('Percentage_6_percentage').value;
if(p1=='') p1=0; if(p2=='') p2=0; if(p3=='') p3=0; if(p4=='') p4=0; if(p5=='') p5=0; if(p6=='') p6=0;
result = parseInt(p1) + parseInt(p2) + parseInt(p3) + parseInt(p4) + parseInt(p5) + parseInt(p6);
if (isNaN(result)) { alert('NaN'); }
else { if(result!=100) {
if ( confirm(result+' is not 100') ) return false; else return false; }
// alert(result+' is not 100'); return false; }
}}",
// 'success' => 'js:function(){window.location="index.php?r=participant/admin"}',
'success'=>'js:function(data){
// if(data.result==="success"){ alert("yes success "); }
if(data.result==="success"){ window.location="index.php?r=participant/admin"; }
else { alert("not success "+data.result); } }',
'error'=>'js:function(data){
if ( typeof(data.msg) === "undefined" ) window.location="index.php?r=participant/admin";
else alert("Error is "+data.msg); }',
),
array('id' => 'mybtn', 'class' => 'class1 class2')); ?>
controller
if ( $model->validate() ) {
$model->save();
...
// $this->render('admin');
// $this->renderPartial('admin','', false, true);
// $this->renderPartial('//participant/admin', array('model'=>new Participant), false, true );
echo CJSON::encode(array( 'status'=>'success', 'result'=>'success' ));
$this->redirect(array('admin'));
// exit(json_encode(array('result' => 'success', 'msg' => 'Your data have been successfully saved')));
// echo CJSON::encode(array( 'status'=>'success', 'result'=>'success' ));
// echo CJSON::encode(array( 'div' => $this->renderPartial('//participant/admin', array('model'=>new Participant), false, true), ));
// echo CJSON::encode(array( 'status'=>'success', 'result'=>'success',
// 'div' => $this->renderPartial('//participant/admin', array('model'=>new Participant), false, true), ));
Yii::app()->end();
return true;
} else {
$msg = $model->getErrors();
echo CJSON::encode(array( 'status'=>'error', 'msg'=>$msg ));
Yii::app()->end();
}