[英]I have patients information in database, now I want to access one's information in another form in Laravel Project
I have patients information in database, now I want to access one's information in another form. 我的数据库中有患者信息,现在我想以其他形式访问一个人的信息。 When I will input patient's ID the patient name field need to be filled/loaded automatically.
当我输入患者的ID时,需要自动填写/加载患者姓名字段。 I have my Patient model also.
我也有我的患者模型。
This is the from... 这是...
<form method="post" class="" action="" >
@csrf
<div class="input-field col s3">
<input name="patient_id" id="id" type="text" class="validate">
<label class="active" for="id">Patient ID</label>
</div>
<div class="input-field col s9">
<input name="patient_name" id="name" type="text" class="validate">
<label class="active" for="name">Patient Name</label>
</div>
<div class="input-field col s12">
<select name="room">
<option value="" disabled selected>Select Room</option>
<option value="2001">ROOM 2001 - Non AC - 500</option>
<option value="4001">ROOM 4001 - AC -800</option>
<option value="301">CABIN 301 - AC - 1700</option>
</select>
</div>
<div class="input-field col s12 text-center">
<button class="btn waves-effect waves-light" type="submit" name="action">Assign Room</button>
</div>
</form>
You can 您可以
create the json object in the blade template, which may be a little messy if special characters are present. 在刀片模板中创建json对象,如果存在特殊字符,可能会有些混乱。 Also, i don't see this as a good solution if you have tons of records.
另外,如果您有大量记录,我也不认为这是一个好的解决方案。
I would use a library, like https://select2.org/ , what you'll be looking for is here https://select2.org/data-sources/ajax ... i personally use this one https://semantic-ui.com/modules/search.html#/examples ... but i think select2 is more straight. 我将使用一个库,例如https://select2.org/ ,您将在这里找到https://select2.org/data-sources/ajax ...我个人使用了这个https://语义 -ui.com/modules/search.html#/examples ...但我认为select2更直接。
At the bottom of https://select2.org/data-sources/ajax you can see how an object with the full data can be passed while querying the server. 在https://select2.org/data-sources/ajax的底部,您可以看到在查询服务器时如何传递具有完整数据的对象。
Since the name is just for searching the id ( I Assume) 由于名称仅用于搜索ID(我假设)
You could also adjust your controller so it looks in CONCAT(id,name) so the user can type the id or name and find the patient. 您还可以调整控制器,使其看起来在CONCAT(id,name)中,以便用户可以键入ID或名称并找到患者。
Pass the patient details to the view
through the compact
and use the values of the patient
collection accordingly in the input fields. 通过
compact
将患者详细信息传递到view
,并在输入字段中相应地使用patient
集合的值。
controller 控制者
public function edit($id){
$patient = Patient::find($id);
return view('patients.edit, compact('patient'))
}
In the view 在视图中
<input value="{{ old('name') ? old('name') : $patient->name }}" name="name" type="text" class="form-control" required>
you can try this. 你可以试试看
Execute a JavaScript when a you Enter patient id in input field: 在输入字段中输入患者ID时执行JavaScript:
here is the example -> https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_ev_oninput 这是示例-> https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_ev_oninput
HTML 的HTML
<input name="patient_id" id="id" type="text" class="validate" oninput="myFunction()">
Javascript Java脚本
function myFunction() {
var p_id = document.getElementById("id").value;
$.ajax({
url: '{{URL::to('get_patient_name')}}'+ '/'+p_id,
type: "get",
success: function (data) {
}
});
}
Now make one route : 现在选择一条路线 :
Route::get('get_patient_name/{p_id}', 'PatientController@getPatientName'});
In controller get the name of the patient 在控制器中获取患者姓名
public function getPatientName($id)
{
return ModelName::where('patient_id',$id)->value('patient_name');
}
and finally, print patient name in the name input field. 最后,在名称输入字段中打印患者姓名。
in the javascript success function 在javascript成功函数中
success: function (data) {
$('#name').val(data);
}
<input name="patient_id" onkeydown="getInfo(this)" id="id" type="text" class="validate">
<script type="text/javascript">
function getInfo(input){
var xhr = new XMLHttpRequest();
xhr.open("GET", 'your/url/to/get/info/from/db/'+input.value, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send();
xhr.onload = function() {
var data = JSON.parse(this.responseText);
document.getElementById('name').value = data.name;
document.getElementById('room').innerHTML= '<option value="'+
data.room +'">'+data.room +'</option>';
data.name
}}
If you want to do it without using jquery, here is vanilla js 如果您想在不使用jquery的情况下进行操作,那么这里是Vanilla js
dont forget to change request url! 不要忘记更改请求网址!
document.querySelector("#id").addEventListener("change", function () {
let xml = new XMLHttpRequest(),
token = document.querySelector("input[name=_token]").value,
id = document.querySelector("#id").value,
name = document.querySelector("#name");
xml.open("get", "get/" + id); // change url
xml.setRequestHeader("X-CSRF-TOKEN", token);
xml.onreadystatechange = function () {
if (xml.readyState === 4) {
var response = JSON.parse(xml.responseText);
if (xml.status === 200 ) {
name.value = response
} else {
console.log('something bad happened');
}
}
};
xml.send()
})
Then in your controller find patient and send back 然后在您的控制器中找到患者并发回
public function getPatient($id){
$patient = Patient::find($id);
return response()->json($patient->name);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.