hello, i’m new come to yii framework and have some problem.
i have register form with ajax valiation like this option in activeform
'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
//'validateOnChange' => true,
),
validation work but when i submit form don’t work.
i want to save information in database and show message to user your info saved and hide form otherwise show error message, i wrote this with json datatype .
this is my ajax button :
echo CHtml::ajaxSubmitButton('عضویت','',array(
'success' =>
'js: function(result) {
if(result != "") {
//if (result.indexOf("{") != 0) {
if (result.error == false)
{
$("#divmsg").removeClass().addClass("alert alert-success");
$("#register").slideUp(700);
}
else
{
$("#divmsg").removeClass().addClass("alert alert-error");
}
$("#divmsg").html(result.msg);
//}
}
}',
'error' =>
'js: function() {
$("#divmsg").removeClass().addClass("alert alert-error");
$("#divmsg").html("خطا در فرستادن اطلاعات");
}',
'beforeSend' =>
'js: function(){
$("#loading").addClass("loading");}',
'complete' =>
'js: function(){
$("#loading").removeClass("loading");}',
'dataType' => 'json',
),
array('class' => 'btn btn-primary'));
and this is my php code to response action
public function actionRegister() {
$model = new User('register');
if (isset($_POST['ajax']) && $_POST['ajax'] === 'register') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
if (isset($_POST['User'])) {
$model->attributes = $_POST['User'];
if (Yii::app()->request->isAjaxRequest) {
header('Content-Type: application/json; charset="UTF-8"');
sleep(2);
if ($model->validate()) {
//here save to database
echo json_encode(array(
'msg' => 'Congratulations!',
'error' => false
));
Yii::app()->end();
}
else
{
echo json_encode(array(
'msg' => $model->getErrors(),
'error' => true
));
Yii::app()->end();
}
}
//else
//$this->redirect(app()->homeUrl);
}
$this->render('register', array('model' => $model));
}
what is the problem in my code ?