如何基于下拉框选择自动生成文本字段

时间:2023-01-27 20:19:36

I have a small form field that has as dropdown <name="product"> this list is generated by pulling data from a mysql table.

我有一个小表单字段,有下拉列表 这个列表是通过从mysql表中提取数据生成的。

The mysql DB is setup as following:

mysql数据库的设置如下:

Table Product:

  Column: Product
  Column: Product ID

Table Contact:

  Column: Product ID
  Column: Contact Name
  Column: Contact name #2
  Column: Contact Name #3
  Column: Contact name #4

Based on what a user selects in the pulldown list, I want to query the database again to datafill 4 new text boxes:

根据用户在下拉列表中选择的内容,我想再次查询数据库以填充4个新文本框:

Name1
Name2
Name3
Name4

Is there a quick and easy way to do this?

有没有快速简便的方法来做到这一点?

The intent of the form will also capture some other data and submit it to a separate database, which leads me into how to tie in two DB's.

表单的意图还将捕获一些其他数据并将其提交到一个单独的数据库,这将引导我如何绑定两个数据库。

Thanks.

1 个解决方案

#1


0  

There are many ways to do this. Here's one:

有很多方法可以做到这一点。这是一个:

  1. When preparing the page, query all the Contact row columns and put them into a JavaScript array on the page. There output could look something like this (there are plenty of correct ways to implement this):

    准备页面时,查询所有Contact行列并将它们放入页面上的JavaScript数组中。输出可能看起来像这样(有很多正确的方法来实现这个):

    <script language=JavaScript>
    var contactInfo = new Array(
      {prodID: 1, name1: 'aaaa', name2: 'bbbb', name3: 'cccc', name4: 'dddd'},
      {prodID: 4, name1: 'eeee', name2: 'ffff', name3: 'gggg', name4: 'hhhh'},
      ...
      {prodID: 90, name1: 'wwww', name2: 'xxxx', name3: 'yyyy', name4: 'zzzz'}
    );
    
  2. In the dropdown list's onchange, call a function to handle the productID selection:

    在下拉列表的onchange中,调用一个函数来处理productID选择:

    <select name="ddlProductID" onchange="processProductChange(this);">
    
  3. Make sure you have the textboxes defined. For this example I'll call them txtName1 through txtName4:

    确保已定义文本框。对于这个例子,我将它们称为txtName1到txtName4:

    <input type=text name="txtName1">
    ...
    <input type=text name="txtName1">
    
  4. In the called function, get the dropdown list's value and locate it in the contactInfo array. That will lead you to the names, and you can pop them into the textboxes:

    在被调用函数中,获取下拉列表的值并在contactInfo数组中找到它。这将引导您到名称,您可以将它们弹出到文本框中:

    function processProductChange(prodDropdown) {
      for (indx = 0; indx < contactInfo.length; ++indx) {
        if (contactInfo[indx].prodID == prodDropdown.value {
           // The product ID has been located in the array. Put its names
           // in the txtName1 through txtName4 textboxes.
           document.forms[0].txtName1.value = contactInfo[indx].name1;
           document.forms[0].txtName2.value = contactInfo[indx].name2;
           document.forms[0].txtName3.value = contactInfo[indx].name3;
           document.forms[0].txtName4.value = contactInfo[indx].name4;
           // all done, can get out
           break;
        }
      }
    }
    

Addendum: OP asked for more detail on this approach. This requires some PHP code, which I'm not that good at, but I know enough that I'll first give the obligatory warning about using the mysql PHP functions. Here goes...

附录:OP要求提供有关此方法的更多详细信息。这需要一些PHP代码,我不擅长,但我知道我会首先给出关于使用mysql PHP函数的强制性警告。开始...

The PHP mysql functions are deprecated. Please use PDO or mysqli instead.

不推荐使用PHP mysql函数。请改用PDO或mysqli。

Now back to the answer. As mentioned, my PHP isn't that good, but basically you want to format the {prodID: ... } lines of the script from step 1 while populating the dropdown list; that way you only need to scan the results once. It would go something like this (interspersed in the code the OP provided in the comment):

现在回到答案。如上所述,我的PHP并不是那么好,但基本上你想在填充下拉列表时格式化步骤1中脚本的{prodID:...}行;这样你只需要扫描一次结果。它会像这样(散布在评论中提供的OP代码中):

<?php
$jsArray = '';
while ($row = mysql_fetch_array($productlist)) {
   $rowmod = strtr($row['ProductID']," ","_"); // change the spaces to underscore to work in URL line
   if (jsArray) {
     // comma between elements
     jsArray .= ",";
   }
   jsArray .= "\n{prodID: {$row['ProductID']}, ";
   jsArray .= "name1: \"{$row['Name1']}\", ";
   jsArray .= "name2: \"{$row['Name2']}\", ";
   jsArray .= "name3: \"{$row['Name3']}\", ";
   jsArray .= "name4: \"{$row['Name4']}\"}";
   echo "<option value='$rowmod'>$row[ProductElement]</option>";
}
?>

Then when you can safely place the JavaScript (say after the closing <form> tag, do this:

然后,当您可以安全地放置JavaScript时(例如在结束

标记之后,执行以下操作:

<? php
echo <<< endJS
 <script language="JavaScript">
  var contactInfo = new Array($jsArray);

  NOTE: PASTE THE FUNCTION FROM STEP 4 HERE

 </script>
endJS;
?>

Chances are there's an error or two in the code here, so some debugging will likely be needed.

这里的代码中可能存在错误或两个错误,因此可能需要进行一些调试。

#1


0  

There are many ways to do this. Here's one:

有很多方法可以做到这一点。这是一个:

  1. When preparing the page, query all the Contact row columns and put them into a JavaScript array on the page. There output could look something like this (there are plenty of correct ways to implement this):

    准备页面时,查询所有Contact行列并将它们放入页面上的JavaScript数组中。输出可能看起来像这样(有很多正确的方法来实现这个):

    <script language=JavaScript>
    var contactInfo = new Array(
      {prodID: 1, name1: 'aaaa', name2: 'bbbb', name3: 'cccc', name4: 'dddd'},
      {prodID: 4, name1: 'eeee', name2: 'ffff', name3: 'gggg', name4: 'hhhh'},
      ...
      {prodID: 90, name1: 'wwww', name2: 'xxxx', name3: 'yyyy', name4: 'zzzz'}
    );
    
  2. In the dropdown list's onchange, call a function to handle the productID selection:

    在下拉列表的onchange中,调用一个函数来处理productID选择:

    <select name="ddlProductID" onchange="processProductChange(this);">
    
  3. Make sure you have the textboxes defined. For this example I'll call them txtName1 through txtName4:

    确保已定义文本框。对于这个例子,我将它们称为txtName1到txtName4:

    <input type=text name="txtName1">
    ...
    <input type=text name="txtName1">
    
  4. In the called function, get the dropdown list's value and locate it in the contactInfo array. That will lead you to the names, and you can pop them into the textboxes:

    在被调用函数中,获取下拉列表的值并在contactInfo数组中找到它。这将引导您到名称,您可以将它们弹出到文本框中:

    function processProductChange(prodDropdown) {
      for (indx = 0; indx < contactInfo.length; ++indx) {
        if (contactInfo[indx].prodID == prodDropdown.value {
           // The product ID has been located in the array. Put its names
           // in the txtName1 through txtName4 textboxes.
           document.forms[0].txtName1.value = contactInfo[indx].name1;
           document.forms[0].txtName2.value = contactInfo[indx].name2;
           document.forms[0].txtName3.value = contactInfo[indx].name3;
           document.forms[0].txtName4.value = contactInfo[indx].name4;
           // all done, can get out
           break;
        }
      }
    }
    

Addendum: OP asked for more detail on this approach. This requires some PHP code, which I'm not that good at, but I know enough that I'll first give the obligatory warning about using the mysql PHP functions. Here goes...

附录:OP要求提供有关此方法的更多详细信息。这需要一些PHP代码,我不擅长,但我知道我会首先给出关于使用mysql PHP函数的强制性警告。开始...

The PHP mysql functions are deprecated. Please use PDO or mysqli instead.

不推荐使用PHP mysql函数。请改用PDO或mysqli。

Now back to the answer. As mentioned, my PHP isn't that good, but basically you want to format the {prodID: ... } lines of the script from step 1 while populating the dropdown list; that way you only need to scan the results once. It would go something like this (interspersed in the code the OP provided in the comment):

现在回到答案。如上所述,我的PHP并不是那么好,但基本上你想在填充下拉列表时格式化步骤1中脚本的{prodID:...}行;这样你只需要扫描一次结果。它会像这样(散布在评论中提供的OP代码中):

<?php
$jsArray = '';
while ($row = mysql_fetch_array($productlist)) {
   $rowmod = strtr($row['ProductID']," ","_"); // change the spaces to underscore to work in URL line
   if (jsArray) {
     // comma between elements
     jsArray .= ",";
   }
   jsArray .= "\n{prodID: {$row['ProductID']}, ";
   jsArray .= "name1: \"{$row['Name1']}\", ";
   jsArray .= "name2: \"{$row['Name2']}\", ";
   jsArray .= "name3: \"{$row['Name3']}\", ";
   jsArray .= "name4: \"{$row['Name4']}\"}";
   echo "<option value='$rowmod'>$row[ProductElement]</option>";
}
?>

Then when you can safely place the JavaScript (say after the closing <form> tag, do this:

然后,当您可以安全地放置JavaScript时(例如在结束

标记之后,执行以下操作:

<? php
echo <<< endJS
 <script language="JavaScript">
  var contactInfo = new Array($jsArray);

  NOTE: PASTE THE FUNCTION FROM STEP 4 HERE

 </script>
endJS;
?>

Chances are there's an error or two in the code here, so some debugging will likely be needed.

这里的代码中可能存在错误或两个错误,因此可能需要进行一些调试。