Codxplore.com

Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif

Yii ajax form submit


Yii ajax form submit

In many cases we need to submit form using jQuery ajax. Yii has built-in support for this funtionality.

In this blog post i will try to cover this topic.

In the view form section at first we need to call form widget

$form = $this->beginWidget('CActiveForm', array(
    'id' => 'formId',
    'enableAjaxValidation' => false,
));

CActiveForm widget accepts multiple parameters, we will use two of them id which will be form unique id. enableAjaxValidation value will be false.

Because we will use custom ajax submit.

Now we will add a form filed to this form


echo $form->labelEx($model,'firstName');
echo $form->textField($model,'firstName');
echo $form->error($model,'firstName');

echo $form->labelEx($model, 'IS_ACT');
echo $form->dropDownList($model, 'IS_ACT', getYesNoArray(), array('class' => 'form-control'));
echo $form->error($model, 'IS_ACT');
.....

After adding the form field we will add the button as follows

echo CHtml::ajaxSubmitButton('Save', CHtml::normalizeUrl(array('yourcontroller/addsetting')),
        array(
            'data' => 'js:jQuery(this).parents("form").serialize()+"&request=ajax"',
            'success' => 'function(data){
                $("#msg").html(data);
            }'
        ),
        array(
            'id' => 'ajaxSubmitBtn',
            'name' => 'ajaxSubmitBtn'
        )
);

Now we need to close the widget using below code

endWidget();

After building the form we need to do integration in controller section, following code we will use for that purpose

public function actionAddSetting()
{
    $model = new AA_APRVL_SETUP();
    if (isset($_POST['AA_APRVL_SETUP'])) {
        $model->attributes = $_POST['AA_APRVL_SETUP'];
        if ($model->save()) {
            echo 'Successfully added';
        }
        else{
            echo CHtml::errorSummary($model);
        }
    }
}

 

Tags : PHP, Yii,

Views : 1134

Subscribe Us


Follow Us