简体   繁体   English

我的数据库中有患者信息,现在我想在Laravel Project中以另一种形式访问一个人的信息

[英]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 您可以

  1. 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. 另外,如果您有大量记录,我也不认为这是一个好的解决方案。

  2. 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.

相关问题 我想获取该信息形式的数组php? - i want to fetch this information form that array php? 我有一个连接到phpmyadmin的laravel网站,但现在我想将android应用程序连接到同一数据库 - I have a laravel website that is connected to phpmyadmin but now I want to connect android app to same database 我有3个表单和一个提交按钮,我只是从1个表单接收信息 - I have 3 forms and one submit button and I am just receiving information from 1 form 我已经在我的数据库中插入了信息,我也验证了它。 但如果我点击插入它重定向到另一页 - i have inserted the information in my database and also i have validate it. but if i click to insert it is redirecting to another page 我想将数组中的字段数据库相互链接,然后显示信息 - I want to database fields in an array to link with each other and then display the information 我有将信息从SQL Server下拉到下拉框中的PHP表单。 我希望将框中的特定项目作为默认选中 - I have PHP form that is pulling information into a dropdown box from SQL Server. I want a particular item in the box as default selected 我如何从数据库中获取信息,就像我将数据与存储的信息插入数据库中以更新数据库一样 - How can I get the information form the database as the same form as I insert the data on it with the stored information in order to update it 我正在开发将信息提交到数据库的AJAX表单。 到达插入功能时记录错误 - I'm developing an AJAX form that submits information to a database. It's logging an error when it reaches the insert function 我有一个PHP表单下拉菜单,需要发送信息 - I have a php form dropdown menu that needs to send information 我有一个登录页面。 我想在表单中添加多个操作,或者有另一种解决方案 - I have a login page. I want to put more than one action in form, or there is another solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM