简体   繁体   English

如何将 JavaScript 变量传递给 yii2 中的 PHP?

[英]How do I pass JavaScript variables to PHP in yii2?

I am developing a recriment software and in Yii2 framework application form I want to check if applicant has already applied for same job using 'passport no', my form looks like我正在开发一个招聘软件,在 Yii2 框架申请表中,我想检查申请人是否已经使用“护照号”申请了相同的工作,我的表格看起来像

 <?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'cnic')->textInput(['type' => 'number']) ?>
<?= Html::submitButton('Save and Continue', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>

and I am unable to get the cnic value in php.而且我无法获得 php 中的 cnic 值。 my Javascript code is.我的 Javascript 代码是。

$('#applications-cnic').change(function(){
    var data = $(this).val();

    var validation = <?= $model->checkCNIC($postid, '<script>data</script>'?>. <?= ) ?>;
    alert(validation);
    if (validation == 'data_exist') {
        alert("You have already applied for this job, use your tracking ID to check your applicaiton or contact HR");
    }
    
});

it is not working and I am unable to pass this value I get from javascript to my php function.它不起作用,我无法将从 javascript 获得的这个值传递给我的 php function。

public function checkCNIC($value , $cnic)
{   

    $query=  Applications::find()
                ->where(['post_id' => $value])
                ->andWhere(['cnic' => $cnic])
                ->all();
                
        if ($query) {
            return 'data_exist';
        }
        else
            return 'no_data';
}

Ajax is the recommended solution. Ajax 是推荐的解决方案。 You can still use javascript inside php using document.writeln您仍然可以使用document.writeln在 php 中使用 javascript

'<script>document.writeln(data)</script>`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM