yii2使用Ajax形式控制Web服务的价值

时间:2018-07-19 08:55:19

标签: ajax yii2 postdata

我想使用带有activeform的表单来创建表单,但是在保存表单数据之前,我要控制Web服务中的数据。所以我使用ajax这个数据控件。

这是 basvurukayit / _form.php

<script>
function myFunction()
{
    //alert("asdsad");
    $.ajax({
        url: '<?php echo Yii::$app->request->baseUrl. '/index.php?r=basvurukayit%2Fkontrol' ?>',
        type: 'post',
        data: {tc:$("#basvurukayit-tc").val() , adsoyad:$("#basvurukayit-adsoyad").val() , babaad:$("#basvurukayit-babaad").val() },
        success: function () {
            $("#kontroldata").val("1");
       },
        error:function () {
            $("#kontroldata").val("0");
       },

  });
}
</script>

<div class="basvurukayit-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'tc')->widget(yii\widgets\MaskedInput::class, ['mask' => '99999999999' ]) ?> 

<?= $form->field($model, 'adsoyad')->textInput(['maxlength' => true, 'style' => 'text-transform: uppercase']) ?> 

<?= $form->field($model, 'babaad')->textInput(['maxlength' => true, 'style' => 'text-transform: uppercase']) ?>

<?= $form->field($model, 'telefon')->widget(yii\widgets\MaskedInput::class, ['mask' => '(999)-999-9999',]) ?>

<?= $form->field($model, 'email')->widget(yii\widgets\MaskedInput::class, [
    'clientOptions' => [ 'alias' =>  'email'  ], 
]) ?>

<?= $form->field($model, 'kursid')->dropDownList(ArrayHelper::map(Basvurukurslar::find()->where('durumu=1')->all(),'id','kodu'), 
    ['prompt' => 'Model Seçiniz',])         ?>

<?= Html::textInput('kontroldata',null,['readonly'=>true,'id'=>'kontroldata','hidden'=>true]); ?>

<div class="form-group">
    <?= Html::submitButton('Kaydet', ['class' => 'btn btn-success','onclick'=>"myFunction()"]) ?>
</div>

<?php ActiveForm::end(); ?>

</div>

我用ajax检查了tc,adsoyad,babaad变量。

basvurukayitcontroller.php

public function actionKontrol()
    { 
        if (Yii::$app->request->isAjax) {        
            $data = Yii::$app->request->post();

            $tc= explode(":", $data['tc']);
            $adsoyad= explode(":", $data['adsoyad']);
            $babaad= explode(":", $data['babaad']);

            $tc= $tc[0];
            $adsoyad= imdat::buyukharf($adsoyad[0]);
            $babaad= imdat::buyukharf($babaad[0]);

            $kpssonuc=imdat::kps($tc);
            //var_dump($kpssonuc);exit;
            if ($adsoyad==$kpssonuc->Ad." ".$kpssonuc->Soyad and 
             $babaad==$kpssonuc->BabaAd) {
                return true;            
            }else{
                return false;            
            }
        }
    } 

此功能有效。测试时,如果我填写正确的值,则响应返回true,否则返回false。

然后,我将此响应分配给隐藏的文本字段#kontroldata。当我删除隐藏属性时,我在#kontroldata中显示响应值。

我使用#kontroldata值检查ajax进程是否有效。因此,我显示诸如“您的信息不正确”或保存的表单数据之类的错误。

但是在完成所有这些过程之后,表单数据已发布,但#kontroldata为空。所以我没有控制ajax进程的成功与否。

basvurukayitcontroller.php

public function actionCreate()
{
    $model = new Basvurukayit();

    if ($model->load(Yii::$app->request->post()) ) {

        echo "<pre>"; var_dump(Yii::$app->request->post()['kontroldata']);exit;

        if (Yii::$app->request->post()['kontroldata']=="1") {
            //$a=Basvurukurslar::find()->where("id=".Yii::$app->request->post()['Basvurukayit']['kursid'])->all();
            $posts = Yii::$app->db->createCommand('SELECT ucreti FROM basvuru_kurslar where id='.Yii::$app->request->post()['Basvurukayit']['kursid'])->queryAll();

            if (count($posts)!=0) {
            $model->kursucret=$posts[0]['ucreti'];
            }else
                $model->kursucret=0;

            $model->userid=Yii::$app->user->identity->id; 

            //echo "<pre>";var_dump($model);exit;

            if ($model->save()) {
                return $this->redirect(['index']);
            }

        }else{
            Yii::$app->session->setFlash('error','Bilgilerde uyuşmazlık tespit edildi.');
            return $this->redirect(['create']);
        }

    }

    return $this->render('create', [
        'model' => $model,
    ]);
}

0 个答案:

没有答案