Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2:基于相关表中的另一个字段自动填充字段_Php_Mysql_Ajax_Yii2 - Fatal编程技术网

Php Yii2:基于相关表中的另一个字段自动填充字段

Php Yii2:基于相关表中的另一个字段自动填充字段,php,mysql,ajax,yii2,Php,Mysql,Ajax,Yii2,我有一个MySQL表和模型patient\u entry,其中包含patient\u name、city和state字段。我还有另一张表/模型健康卡,其中还包含患者姓名、城市和州 假设patient\u条目表中已填入patient\u姓名、城市和州 当我在健康卡表单中输入数据时,当我通过与患者条目表相关的下拉字段选择患者姓名时,我希望相关的城市和州字段自动填充 我的健康卡的\u form.php如下所示: <head> <script> $thi

我有一个MySQL表和模型
patient\u entry
,其中包含
patient\u name
city
state
字段。我还有另一张表/模型
健康卡
,其中还包含
患者姓名
城市

假设
patient\u条目
表中已填入
patient\u姓名
城市

当我在
健康卡
表单中输入数据时,当我通过与
患者条目
表相关的下拉字段选择
患者姓名
时,我希望相关的
城市
字段自动填充

我的
健康卡
\u form.php
如下所示:

    <head>
<script>

        $this->registerJs("$('#healthcard-patient_name').on('change',function(){
    $.ajax({
        url: '".yii\helpers\Url::toRoute("HealthCard/patient")."',
        dataType: 'json',
        method: 'GET',
        data: {id: $(this).val()},
        success: function (data, textStatus, jqXHR) {
            $('#healthcard-city').val(data.city);
            $('#healthcard-pincode').val(data.pin);
        },
        beforeSend: function (xhr) {
            alert('loading!');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('An error occured!');
            alert('Error in ajax request');
        }
    });
});"); 

      </script>
</head>

您所需要的只是调用一个
AJAX
请求来获取所需的字段。就这样做吧:

$this->registerJs("$('#patient-patient_name').on('change',function(){
    $.ajax({
        url: '".yii\helpers\Url::toRoute("controllerName/patient")."',
        dataType: 'json',
        method: 'GET',
        data: {id: $(this).val()},
        success: function (data, textStatus, jqXHR) {
            $('#patient-city').val(data.city);
            $('#patient-state').val(data.state);
        },
        beforeSend: function (xhr) {
            alert('loading!');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('An error occured!');
            alert('Error in ajax request');
        }
    });
});"); 
  • (我不知道您的型号名称)查看您的表单,查看您的
    患者姓名
    字段的
    id
    。它通常是
    modelname字段名
    。我假设您的型号名称是
    患者
    。因此,
    patient\u name
    的id将是
    patient-patient\u name

  • 添加一个ajax请求(在您的视图中)

  • 调用AJAX的代码可能如下所示:

    $this->registerJs("$('#patient-patient_name').on('change',function(){
        $.ajax({
            url: '".yii\helpers\Url::toRoute("controllerName/patient")."',
            dataType: 'json',
            method: 'GET',
            data: {id: $(this).val()},
            success: function (data, textStatus, jqXHR) {
                $('#patient-city').val(data.city);
                $('#patient-state').val(data.state);
            },
            beforeSend: function (xhr) {
                alert('loading!');
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log('An error occured!');
                alert('Error in ajax request');
            }
        });
    });"); 
    
    注:

    • 用您自己的代码更改上述代码中的控制器名称
    • 我假设
      城市
      字段的id具有以下id:
      患者城市
      州城市
    • 患者是控制器中的一个动作
    • 您可能需要删除警报日志,并对上述代码进行一些自定义
    • 我没有考虑任何代码清理的条件。请确保用户数据正确无误

    • 最后,将操作代码添加到控制器中
    行动代码:

    public function actionPatient($id){
        // you may need to check whether the entered ID is valid or not
        $model=  \app\models\Patient::findOne(['id'=>$id]);
        return \yii\helpers\Json::encode([
            'city'=>$model->city,
            'state'=>$model->state
        ]);
    }
    

    亲爱的阿里,非常感谢你。只需要一点澄清-例如,我的控制器名称是
    HealthCardController
    ,所以我应该写
    HealthCardController/patient
    还是只写
    HealthCardController/patient
    ,查看代码应该放在脚本标签下的标题部分。亲爱的,不客气。这是正确的:
    健康卡/患者
    。关于脚本标记,不需要将其放入脚本标记中。当你说
    $this->registerJs
    时,我会自动将其放入
    标记中。亲爱的Ali,我已经按照你的建议编辑了这个问题,但看起来我仍然缺少一些内容。亲爱的,如果我将其移到脚本标记之外,代码将显示在前面的视图中。应该在打开php标记之后。没有错误。嗨,阿里,我正在尝试使用这个自动填充的一个小修改版本。我已经为输入创建了一个html文本字段(general_regn_no)。在另一个模型中有一个同名字段。这很有效。我还在文本字段旁边放置了一个按钮,我希望在单击此按钮时激活搜索。如何修改此行以使其适用于单击按钮事件:
    $this->registerJs($(“#患者-患者名称”)。on('change',function()