簡體   English   中英

使用PHP和MySQL如何使用另一個表中的某些字段值填充一個表

[英]Using PHP and MySQL how do I populate one table using some field values from another table

我正在一個數據庫項目上,需要使用選擇框在其中一個表單表中填充來自另一個表的一些值。

表一稱為reg_table ,具有以下字段:

RegNo | Surname | Othernames | Assembly
-------------------------------------------------
00001 | John    | Okon       | Glory Assembly
00002 | Peter   | Dan        | Shepherds Assembly
00003 | Ada     | Victor     | Pnuema Assembly

表二稱為valparts_table ,將以下內容合在一起將表一的所有字段如下所示

CountID  | MTNumber | Camping | Hostel    |  RegNo | Surname | Othernames | Assembly
----------------------------------------------------------------------------------------------
00001    | 0002     | Yes     | Hostel-1  |  00001 | John    | Okon       | Glory Assembly
00002    | 0003     | No      | Hostel-2  |  00002 | Peter   | Dan        | Shepherds Assembly
00003    | 0004     | Yes     | Hostel-3  |  00003 | Ada     | Victor     | Pnuema Assembly
  1. 現在,當您打開valpartsfrm表單並使用鏈接到reg_table的valpartsfrm表單上的選擇框對象選擇姓氏值時,它將顯示所有姓氏。 當您從列表中選擇一個姓氏時,應使用reg_table中的RegNo姓氏其他名稱程序集的值填充valpartsfrm上的以下字段。

  2. 完成此操作后,您可以手動填寫剩余的valpartsfrm表單字段(MTNumber,Camping Hostel)

請注意, valparts_table綁定到valpartsfrm表單。

鏈接http://dbms.rmww.org/images/validatepart.jpg顯示了使用MS Access 2007開發的數據庫的示例

我想在選擇選項時填寫表格,請嘗試以下代碼

它的形式演示

在此選擇框中具有json字符串中的選項值和onchange函數

<table>
<tr>
    <td>Select Surname</td>
    <td>
        <select id="my_select" onchange="javascript:fill_form(this.value)">
            <option value="">Select</option>
            <?php
            $res = mysql_query("SELECT * FROM `reg_table` ") or die(mysql_error());
            while($row = mysql_fetch_assoc($res))
            {
              $key = json_encode($row);
              echo "<option value='".$key."'>".$row['Surname']."</option>";
            }
            ?>
        </select>
    </td>
</tr>

<tr>
    <td>RegNo</td>
    <td>
        <input type="text" value="" name="RegNo" id="RegNo" />
    </td>
</tr>

<tr>
    <td>Surname</td>
    <td>
        <input type="text" value="" name="Surname" id="Surname" />
    </td>
</tr>

<tr>
    <td>Othernames</td>
    <td>
        <input type="text" value="" name="Othernames" id="Othernames" />
    </td>
</tr>

<tr>
    <td>Assembly</td>
    <td>
        <input type="text" value="" name="Assembly" id="Assembly" />
    </td>
</tr>
</table>

JS

當您更改此json字符串中的選擇框選項時,此js函數調用在對象中被定位並相應地填充文本框

<script>
function fill_form(json_string)
{   

   if(json_string=="")
   {
        document.getElementById('RegNo').value = "";
        document.getElementById('Surname').value = "";
        document.getElementById('Othernames').value = "";
        document.getElementById('Assembly').value = "";
   }
   else
   {
        var jsonData = JSON.parse(json_string); // json string to object 
        document.getElementById('RegNo').value = jsonData.RegNo;
        document.getElementById('Surname').value = jsonData.Surname;
        document.getElementById('Othernames').value = jsonData.Othernames;
        document.getElementById('Assembly').value = jsonData.Assembly;
   }
}
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM