Php Yii2:基于相关表中的另一个字段自动填充字段
我有一个MySQL表和模型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
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()