猿问

如何通过动态表单获取所选 ID 的列?

我有一个包含 2 个选择字段和 1 个输入字段的小脚本。第一个选择框从一个表中拉取,一旦从中选择了一个项目,它就会用从第二个表中拉取的内容填充第二个选择框。

我需要的是第三个输入框,以显示与选择有关的下一列。

表 1 包含 2 列(CAT_ID 和 CADESC)

表 2 包含 4 列(SUB_ID、CAT_ID、SUBCATDESC 和 NAME)

因此,当我在第一个下拉框中选择一个选项时,它会匹配表 1 和表 2 中的 CAT_ID,并在第二个下拉框中插入 SUBCATDESC。

但我不知道如何使用给定 CAT_ID > SUBCAT_ID 的 NAME 预填充输入字段。

希望有人能插话。

请注意,下面的代码已从工作环境中剥离,我只是想在工作表单中添加一个额外的字段。

<script type="text/javascript">

function AjaxFunction()

{

var httpxml;

try

 {

 // Firefox, Opera 8.0+, Safari

 httpxml=new XMLHttpRequest();

 }

catch (e)

 {

 // Internet Explorer

      try

                    {

                 httpxml=new ActiveXObject("Msxml2.XMLHTTP");

                }

            catch (e)

                {

            tray

            {

            httpxml=new ActiveXObject("Microsoft.XMLHTTP");

             }

            catch (e)

            {

            alert("Your browser does not support AJAX!");

            return false;

            }

        }

 }

function stateck() 

   {

   if(httpxml.readyState==4)

     {

//alert(httpxml.responseText);

var myarray = JSON.parse(httpxml.responseText);

// Remove the options from 2nd dropdown list 

for(j=document.contactform.subcat.options.length-1;j>=0;j--)

{

document.contactform.subcat.remove(j);

}

for (i=0;i<myarray.data.length;i++)

{

var optn = document.createElement("OPTION");

optn.text = myarray.data[i].subcategory;

optn.value = myarray.data[i].subcategory;  // You can change this to subcategory 

document.contactform.subcat.options.add(optn);


     }

   } // end of function stateck

var url="dd.php";

var cat_id=document.getElementById('s1').value;

url=url+"?cat_id="+cat_id;

url=url+"&sid="+Math.random();

httpxml.onreadystatechange=stateck;

//alert(url);

httpxml.open("GET",url,true);

httpxml.send(null);

 }


潇湘沐
浏览 180回答 1
1回答

幕布斯6054654

我不是 SQL 专家,但我知道一点 PHP。也许这会有所帮助。得到两个表。从第一个表中获取第一行,作为默认值,并存储它。然后循环第二个表并将第一个表中的每一行与第二个表中的行进行比较。当cat_id两行中的a相同时,将存储第二行。将FIRSTTABLE和SECONDTABLE字符串更改为您自己的表名<?php// Query to collect data table 1.// Change the FIRSTTABLE and SECONDTABLE to your table names.$table_1_sql = "select * from FIRSTTABLE";&nbsp;&nbsp;$table_2_sql = "select * from SECONDTABLE";// Query both tables$query_table_1 = $dbo->query( $table_1_sql );$query_table_2 = $dbo->query( $table_2_sql );// Set the initial values to false.$selected_row_1 = false;$selected_row_2 = false;// Loop over the first table to get the first row.foreach( $query_table_1 as $row_table_1 ) {&nbsp; if ( $selected_row_1 === false ) {&nbsp; &nbsp; $selected_row_1 = $row_table_1&nbsp; }}// Loop over the second table to get the row that matches that cat_id from the first table row.foreach( $query_table_2 as $row_table_2 ) {&nbsp; &nbsp;if ( $selected_row_1 !== false ) {&nbsp; &nbsp; &nbsp; if ( $selected_row_1[ 'cat_id' ] === $row_table_2[ 'cat_id' ] ) {&nbsp; &nbsp; &nbsp; &nbsp; $selected_row_2 = $row_table_2;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}// The values from the first and second row.// Both rows with the same 'cat_id' value.var_dump( $selected_row_1 ); // array( 'cat_id' => SOMENUMBER, 'catdesc' => 'SOMEDESCRIPTION' );var_dump( $selected_row_2 ); // array( 'cat_id' => SOMENUMBER, 'sub_id' => SOMENUMBER, 'catdesc' => 'SOMEDESCRIPTION', 'name' => 'SOMENAME' );?>现在您应该拥有填充字段所需的数据。该$selected_row_2变量现在包含一个数组,其名称是您的输入字段所需的名称。免责声明这一切都是基于您提供的小信息,我希望它会帮助您。如果没有,我会尽我所能带你去你需要去的地方。
随时随地看视频慕课网APP
我要回答