简体   繁体   English

根据HTML下拉选择填充输入

[英]Populating Inputs Based on HTML Dropdown Selection

I know there are many questions like this one on Stack Overflow, but I am really needing something more related towards what I am doing...so that is why I am posting this question. 我知道在Stack Overflow上有很多类似的问题,但是我确实需要与我正在做的事情相关的更多信息...所以这就是为什么我要发布此问题。

I have an HTML form with PHP that pulls data from an MSSQL database in order to populate a dropdown box. 我有一个带PHP的HTML表单,该表单从MSSQL数据库中提取数据以填充下拉框。 I want to be able to populate the contact information text boxes once I select a name from my dropdown box. 从下拉框中选择一个姓名后,我希望能够填充联系人信息文本框。 So far, I have it to where, if I select a specific vendor, the vendor name gets entered automatically into the Name, Email, and Phone Number fields. 到目前为止,如果选择了特定的供应商,则可以到达该位置,该供应商名称会自动输入到“名称”,“电子邮件”和“电话号码”字段中。 However, I want to have different attributes entered into the text fields and not the vendor name itself. 但是,我想在文本字段中输入不同的属性,而不是供应商名称本身。 How could I do this? 我该怎么办?

The fields from my database with the name, email, and phone number that I want imported are named MR_POC_N, MR_POC_E, and MR_POC_P 我要导入的数据库中名称,电子邮件和电话号码的字段分别命名为MR_POC_N,MR_POC_E和MR_POC_P

Also the "MR_Name" that you see is the field with all names of the vendors FYI 同样,您看到的“ MR_Name”是包含供应商FYI所有名称的字段

Here is my code for the HTML dropdown: 这是我的HTML下拉菜单代码:

<select name="vendor_dropdown" id="ven" onChange="updateinput();">
    <option value="">Choose a Vendor</option>
        <?php foreach($users->fetchAll() as $user): ?>
            <option><?php echo $user['MR_Name'];?></option>
        <?php endforeach; ?>
</select>

Here is my Contact Info code: 这是我的联系信息代码:

<table align="center">
    <tr>
        <td align="right">Name:</td>
        <td><input class="textbox" type="text" id="name" name="name"></td>
    </tr>
    <tr>
        <td align="right">Email:</td>
        <td><input class="textbox" type="email" id="email" name="email"></td>
    </tr>
    <tr>
        <td align="right">Phone Number:</td>
        <td><input class="textbox" type="tel" id="tel" name="number"></td>
    </tr>
</table>

Here is the update input function I have in JS. 这是我在JS中使用的更新输入函数。 I know this is off but looking for some sort of direction as I am not the best in JS: 我知道这是关闭的,但是由于我不是JS最好的人,所以正在寻找某种方向:

function updateinput() {


    var e = document.getElementById("ven");
        var venSelected = e.options[e.selectedIndex].value;

        document.getElementById("name").value=venSelected;
        document.getElementById("email").value=venSelected;
        document.getElementById("tel").value=venSelected;
    }

Keep your script the same as it currently is but change your JavaScript and the dropdown. 保持脚本与当前相同,但更改JavaScript和下拉菜单。 In your dropdown try doing 在下拉菜单中尝试

<option value = "<?=$user['MR_Name'];?>"><?php echo $user['MR_Name'];?></option>

You want to make what's called an AJAX request. 您要发出所谓的AJAX请求。 This will allow you to go off to another page get some data in your case you would be getting some user information, and return it to the page, without the user physically changing page! 如果您要获取一些用户信息,则可以转到另一个页面以获取一些数据,然后将其返回给页面,而无需用户实际更改页面!

Check this link out for more information on AJAX. 检查链接以获取有关AJAX的更多信息。

Your AJAX call will look something like this. 您的AJAX呼叫将如下所示。

function updateinput() {
    var e = $("#ven").val();

    $.ajax({
        url: "get-user-info.php",
        type: "POST",
        data: {e: e},
        success: function(data){
            data = JSON.parse(data);

            var email = data[0];
            var tel = data[1];

            $("#email").val(email);
            $("#tel").val(tel);
        }
    });
}

This script first gets the selected value of the dropdown. 该脚本首先获取下拉菜单的选定值。 Then it starts the make the AJAX call, it's like submitting a form, we are POSTING the values across to the next page. 然后,它开始进行AJAX调用,就像提交表单一样,我们将值发布到下一页。 So the page the AJAX will talk to is get-user-info.php it's posting the values from data. 因此,AJAX将与之交谈的页面是get-user-info.php它正在发布数据中的值。 The data is sending across e which is what the dropdown value is. 数据通过e发送,这就是下拉值。 On success it will then do the code underneath which is saying parse data from a PHP array to JavaScript and then it is saying email is the first value of the array and tel is the second value from the array. 成功后,它将执行下面的代码,即将数据从PHP数组解析为JavaScript,然后说电子邮件是数组的第一个值,而tel是数组的第二个值。 Then we are saying the input with ID 'email' now equals email from the array and the input with ID 'tel' now equals tel from the array. 然后,我们说ID为'email'的输入现在等于来自数组的email ,ID为'tel'的输入现在等于来自数组的tel

You will now need to create a php script in the same directory called get-user-info.php 现在,您需要在同一目录中创建一个名为get-user-info.php的php脚本。

In this script make a call to your database where the user is the same as the selected value e and return a PHP array that looks like this: 在此脚本中,调用用户与所选值e相同的数据库,并返回如下所示的PHP数组:

array(email, telephone)

This is pseudo-code for your PHP script get-user-info.php : 这是您的PHP脚本get-user-info.php伪代码:

<?php

$e = $_POST["e"];
//Do your SQL

//In the loop
$array[] = $user["email"];
$array[] = $user["tel"];

echo json_encode($array);
?>

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

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