gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:42:07 25 4
gpt4 key购买 nike

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

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

表1由 2 列组成(CAT_ID 和 CATDESC)

表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);
}
</script>
<?php require "config.php"; ?>
<form name="contactform" method="post" action="" id="contactform">
<select name="cat" id='s1' onchange=AjaxFunction(); required>
<option value=''></option>
<?php
$sql="select * from testtable"; // Query to collect data from table
foreach ($dbo->query($sql) as $row) { ?>
<option value=<?php echo "$row[cat_id]"; ?>><?php echo "$row[category]"; ?></option>
<?php } ?>
</select>
<select name="subcat" id='s2' style="width:200px;"></select>
<input name="name" id="name" type=text required >
<input value="send email" type=submit id="sendemail">
<?php
if(isset($_POST['subcat'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = $_POST['subcat'];
$email_subject = "Request";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}


// validation expected data exists
if( !isset($_POST['s1']) ||
!isset($_POST['s2']) ||
!isset($_POST['name'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$s1 = $_POST['s1']; // required
$s2 = $_POST['s2']; // required
$name = $_POST['name']; // required

$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "S1: ".clean_string($s1)."\n";
$email_message .= "S2: ".clean_string($s2)."\n";
$email_message .= "Name: ".clean_string($name)."\n";

// create email headers
$headers = 'From: '."test@test.com"."\r\n";
$headers .= 'Reply-To: '."test@test.com"."\r\n";
$headers .= 'CC: '."testing@test.com"."\r\n";
$headers .= 'MIME-Version: 1.0\r\n';
$headers .= 'Content-Type: text/html; charset=ISO-8859-1\r\n';
@mail($email_to, $email_subject, $email_message, $headers);
?>
<div><font color=green><b>Your request has been sent.</b></div>
<?php } ?>
</form>

最佳答案

我不是 SQL 专家,但我了解一点 PHP。也许这会有所帮助。获取两张 table 。您想要从第一个表中获取第一行作为默认值,并将其存储。然后循环第二个表,并将第一个表中的每一行与第二个表中的行进行比较。当两行中的 cat_id 相同时,将存储第二行。

FIRSTTABLESECONDTABLE 字符串更改为您自己的表名称

<?php

// Query to collect data table 1.
// Change the FIRSTTABLE and SECONDTABLE to your table names.
$table_1_sql = "select * from FIRSTTABLE";
$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 ) {
if ( $selected_row_1 === false ) {
$selected_row_1 = $row_table_1
}
}

// 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 ) {
if ( $selected_row_1 !== false ) {
if ( $selected_row_1[ 'cat_id' ] === $row_table_2[ 'cat_id' ] ) {
$selected_row_2 = $row_table_2;
}
}
}

// 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 变量现在保存一个数组,其中包含输入字段所需的名称。

免责声明

这一切都是基于您提供的一点信息,希望对您有所帮助。如果没有,我会尽力帮助您到达您需要的地方。

关于javascript - 如何通过动态表单获取所选ID的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57510416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com