Ajax form submits twice in yii 1.1

I have an ajax form which gets submitted twice and hence same record being replicated twice in the db.

This particular cases happens only when the Client side validation is turned on.

I have also tried using the e.stopImmediatePropagation() but this will disable the error messages from being displayed as well which is not a solution in my case.

I want both the verification and the form submission to work fine without replication.

Here’s my code:


public function actionIndex() {

    $model = new Transaction();

    if (isset($_POST['ajax']) && $_POST['ajax'] === 'transaction-form') {

        echo CActiveForm::validate($model);



    if (Yii::app()->request->isAjaxRequest) {

        $model->attributes = $_POST['Transaction'];


            echo 'Success';




    $this->render('index', array('model' => $model));


View File:

<div class="form">


$form = $this->beginWidget('CActiveForm', array(

'id' => 'transaction-form',

'action' => Yii::app()->createUrl('transaction/index'),



'clientOptions' => array(

    'validateOnSubmit' => true,




<p class="note">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>

<div class="row">

    <?php echo $form->labelEx($model,'BilledAmount'); ?>

    <?php echo $form->textField($model,'BilledAmount',array('size'=>10,'maxlength'=>10)); ?>

    <?php echo $form->error($model,'BilledAmount'); ?>


<div class="row">

    <?php echo $form->labelEx($model,'ChargedAmount'); ?>

    <?php echo $form->textField($model,'ChargedAmount',array('size'=>10,'maxlength'=>10)); ?>

    <?php echo $form->error($model,'ChargedAmount'); ?>


<div class="row">

    <?php echo $form->labelEx($model,'CardExpiry'); ?>

    <?php echo $form->textField($model,'CardExpiry',array('size'=>4,'maxlength'=>4)); ?>

    <?php echo $form->error($model,'CardExpiry'); ?>


<div class="row buttons">

     <input name="submit" type="submit" value="Submit">


<?php $this->endWidget(); ?>

</div><!-- form -->


  $(function () {

  $('form#transaction-form').on('submit', function (e) {


  var action = $("#transaction-form").attr('action');

  var datas = $("#transaction-form").serialize();


    type: 'post',

    url: action,

    data: datas,

    success: function (msg) {

      if(msg == 'Success'){





  return false;


You should use ‘afterValidate’ option to submit the form using $.ajax function. You can read about an example ajax form submission here http://databytebank.com/yii/yii-ajax-form-submission/

You should register the script http://www.yiiframework.com/wiki/800/how-to-register-scripts-and-css/