2016-01-06 41 views
1

我有一個ajax表單,它被提交了兩次,因此同樣的記錄在db中被複制兩次。Ajax表單在yii中提交兩次1.1

這種特殊情況只有在打開客戶端驗證時纔會發生。

我也嘗試過使用e.stopImmediatePropagation(),但是這將禁止從顯示的錯誤消息以及這不是我的情況下的解決方案。我希望驗證和表單提交都能正常工作而不需要複製。

這裏是我的代碼:

控制器:

public function actionIndex() { 

    $model = new Transaction(); 

    if (isset($_POST['ajax']) && $_POST['ajax'] === 'transaction-form') { 
     echo CActiveForm::validate($model); 

     Yii::app()->end(); 
    } 

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

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

     if($model->save()){ 
      echo 'Success'; 
     } 

     Yii::app()->end(); 
    } 

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

查看文件:

<div class="form"> 
<?php 
$form = $this->beginWidget('CActiveForm', array(
'id' => 'transaction-form', 
'action' => Yii::app()->createUrl('transaction/index'), 
'enableAjaxValidation'=>true, 
'enableClientValidation'=>true, 
'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> 

<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> 

<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> 

<div class="row buttons"> 
    <input name="submit" type="submit" value="Submit"> 
</div> 

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

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

<script> 
    $(function() { 

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

    e.preventDefault(); 

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

    $.ajax({ 
    type: 'post', 
    url: action, 
    data: datas, 
    success: function (msg) { 

     if(msg == 'Success'){ 
      location.reload(); 
     } 
    } 
    }); 


    return false; 

}); 

回答

0

你的行動應該是這樣的:

public function actionIndex() { 
    $model = new Transaction(); 
    if (Yii::app()->getRequest()->getIsAjaxRequest()) { 
     echo CActiveForm::validate($model); 
     Yii::app()->end(); 
    } 
    if (isset($_POST['Transaction'])) { 
     $model->attributes = $_POST['Transaction']; 
     if($model->save()){ 
     Yii::app()->user->setFlash('success', 'Transaction saved.'); 
       $this->refresh(); 
     }else{ 
      Yii::app()->user->setFlash('error', 'Transaction saving error.'); 
     } 
    } 
    $this->render('index', array('model' => $model)); 
} 
+0

感謝您的意見。但它沒有效果,也沒有解決問題。 – user96675