我有一个组合框,它从表中收集字段,我想使用选定的 id 在不同的文本框中显示其他值。 get Element By Id 仅返回单个字段。
<?php
$druglquery = "SELECT ID,DrugName,DrugForm FROM DrugsInformation ";
$druglresult = $mysqli->druglquery($druglquery) ; //onchange='submitForm();'
?>
<select style='width:242px;' id="dcodeID" onchange="onselectchange();">
<option value=''></option>
<?php
$row = $druglresult->fetch_array(MYSQLI_BOTH);
printf ("%s (%s)\n", $row[0], $row["DrugName"]);
?>
</select>
<script>
function onselectchange()
{
var d=document.getElementById("dcodeID")
var diplaytext=d.options[d.selectedIndex].text;
document.getElementById("test").value=diplaytext;
alert(" ok ok");
}
</script>
使用相同的ID我想要像下面这样的东西
<script>
function onselectchange()
{
var d=document.getElementById("dcodeID")
var diplaytext=d.options[d.selectedIndex].text;
document.getElementById("test").value=diplaytext;
document.getElementById("test2").value=diplaytext;
document.getElementById("test3").value=diplaytext;
alert(" ok ok");
}
</script>
如果我理解正确,那么我希望以下内容可以提供一些指导来帮助解决您在实际代码中遇到的问题。
<?php
# a static array to emulate a basic recordset
# this is just dummy data
$drugs=array(
array('id'=>1, 'name'=>'Abutilon','price'=>'25', 'form'=>'tablet', 'quantity'=>50, 'manufacturer'=>'ACME Drugs Corp' ),
array('id'=>2, 'name'=>'Pramaxil','price'=>'50', 'form'=>'injection', 'quantity'=>20, 'manufacturer'=>'ACME Cybernautics Division' ),
array('id'=>3, 'name'=>'Exylichnine','price'=>'150', 'form'=>'suppository', 'quantity'=>30, 'manufacturer'=>'ACME Corporate Greed Division' ),
array('id'=>4, 'name'=>'BoronHydroxil','price'=>'55', 'form'=>'cream', 'quantity'=>1, 'manufacturer'=>'ACME Famine Feasibility' ),
array('id'=>5, 'name'=>'Dexaclam','price'=>'10', 'form'=>'tablet', 'quantity'=>100, 'manufacturer'=>'ACME Drugs Corp' )
);
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<script>
document.addEventListener('DOMContentLoaded',e=>{
let oForm=document.forms.admin;
document.querySelector('select[name="drugs"]').addEventListener('change',function(e){
let option=this.options[this.options.selectedIndex];
oForm.price.value=option.dataset.price;
oForm.quantity.value=option.dataset.quantity;
oForm.form.value=option.dataset.form;
oForm.manufacturer.value=option.dataset.manufacturer;
});
});
</script>
</head>
<body>
<form name='admin' method='post'>
<table>
<tr>
<td>
<select name='drugs'>
<option selected hidden disabled>Please select your drug
<?php
/* emulate iterating through recordset */
foreach( $drugs as $arr ){
printf(
'<option value="%d" data-price="%s" data-quantity="%d" data-manufacturer="%s" data-form="%s">%s',
$arr['id'],
$arr['price'],
$arr['quantity'],
$arr['manufacturer'],
$arr['form'],
$arr['name']
);
}
?>
</select>
</td>
<td>
<label>Price: <input type='text' name='price' /></label>
</td>
<td>
<label>Quantity: <input type='text' name='quantity' /></label>
</td>
<td>
<label>Form: <input type='text' name='form' /></label>
</td>
<td>
<label>Manufacturer: <input type='text' name='manufacturer' /></label>
</td>
</tr>
</table>
</form>
</body>
</html>
document.addEventListener('DOMContentLoaded',e=>{
let oForm=document.forms.admin;
document.querySelector('select[name="drugs"]').addEventListener('change',function(e){
let option=this.options[this.options.selectedIndex];
oForm.price.value=option.dataset.price;
oForm.quantity.value=option.dataset.quantity;
oForm.form.value=option.dataset.form;
oForm.manufacturer.value=option.dataset.manufacturer;
});
});
<form name='admin' method='post'>
<table>
<tr>
<td>
<select name='drugs'>
<option selected hidden disabled>Please select your drug
<option value="1" data-price="25" data-quantity="50" data-manufacturer="ACME Drugs Corp" data-form="tablet">Abutilon
<option value="2" data-price="50" data-quantity="20" data-manufacturer="ACME Cybernautics Division" data-form="injection">Pramaxil
<option value="3" data-price="150" data-quantity="30" data-manufacturer="ACME Corporate Greed Division" data-form="suppository">Exylichnine
<option value="4" data-price="55" data-quantity="1" data-manufacturer="ACME Famine Feasibility" data-form="cream">BoronHydroxil
<option value="5" data-price="10" data-quantity="100" data-manufacturer="ACME Drugs Corp" data-form="tablet">Dexaclam
</select>
</td>
<td>
<label>Price: <input type='text' name='price' /></label>
</td>
<td>
<label>Quantity: <input type='text' name='quantity' /></label>
</td>
<td>
<label>Form: <input type='text' name='form' /></label>
</td>
<td>
<label>Manufacturer: <input type='text' name='manufacturer' /></label>
</td>
</tr>
</table>
</form>
查看我之前提供的答案的修改版本,我相信您正在尝试做这样的事情。在前面的示例中,我只使用了静态数组,以便我可以模拟/模仿典型的记录集,而不是作为解决方案本身,毕竟,这确实是 Javascript!
但是,在修改后的代码中,您将记录集中的值分配给
$arr
变量,但在记录集的每次迭代中,您只是覆盖了先前的值,这意味着在循环结束时数组中将只有一个维度.
当您处理记录集时,这个阶段是不必要的(记住我只使用数组来模拟记录集) - 迭代
$results
并像以前一样直接生成菜单(为了方便和清晰,我喜欢printf
)
<?php
error_reporting( E_ALL );
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = 'xxx';
$dbname = 'drugmgmnt';
mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
$mysqli= new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$mysqli->set_charset('utf8');
$DrugName=filter_input(INPUT_GET,'DrugName',FILTER_SANITIZE_STRING);
$DrugForm=filter_input(INPUT_GET,'DrugForm',FILTER_SANITIZE_STRING);
$Strength=filter_input(INPUT_GET,'Strength',FILTER_SANITIZE_STRING);
$Units=filter_input(INPUT_GET,'Units',FILTER_SANITIZE_STRING);
$QtyInPack=filter_input(INPUT_GET,'QtyInPack',FILTER_SANITIZE_NUMBER_INT);
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<script>
document.addEventListener('DOMContentLoaded',e=>{
let oForm=document.forms.admin;
document.querySelector('select[name="drug"]').addEventListener('change',function(e){
let option=this.options[this.options.selectedIndex];
oForm.Strength.value=option.dataset.strength;
oForm.Units.value=option.dataset.units;
oForm.DrugForm.value=option.dataset.drugform;
oForm.PackType.value=option.dataset.packtype;
});
});
</script>
</head>
<body>
<form name='admin' method='post'>
<table>
<tr>
<td>
<select name='drug'>
<option >Please select your drug
<?php
$sql = "select
`id`,
`drugname`,
`strength`,
`drugform`,
`units`,
`packtype`
from `drugsinformation`";
$result = $mysqli->query( $sql );
while( $row = $result->fetch_assoc() ){
printf(
'<option value="%d" data-strength="%d" data-units="%d" data-packtype="%s" data-drugform="%s">%s',
$row['id'],
$row['strength'],
$row['units'],
$row['packtype'],
$row['drugform'],
$row['drugname']
);
}
}
?>
</select>
</td>
<td>
<label>Strength: <input type='text' name='Strength' /></label>
</td>
<td>
<label>Units: <input type='text' name='Units' /></label>
</td>
<td>
<label>DrugForm: <input type='text' name='DrugForm' /></label>
</td>
<td>
<label>PackType: <input type='text' name='PackType' /></label>
</td>
</tr>
</table>
</form>
</body>
</html>
<form id="customForm" method="get" action="" >
<?php
require_once('../connection/config.php');
?>
<td width="" valign="top"><span class="mandatory">*</span> Drug Name <br>
<div><?php
if ($SavedDrug !="")
{
$DLNames=Getdrugname($SavedDrug);
?>
<div class="notice">
<?php
echo '<strong>'.$DLNamed.'</strong>';
echo"<input name='dcode' type='hidden' value='$SavedDrug' />";?>
</div>
<?php
}
else
{ ?>
<?php
$druglquery = "SELECT ID,DrugName,DrugForm FROM DrugsInformation ";
$druglresult = $mysqli->query($druglquery) ; //onchange='submitForm();'
?>
</div>
<select style='width:242px;' id="dcodeID" onchange="onselectchange();">
<option value=''></option>
<?php
$row = $druglresult->fetch_array(MYSQLI_BOTH);
{ $dID = $row['ID'];
$dname = $row['DrugName'];
$dform = $row['DrugForm'];
?>
<option value="<?php echo $dID; ?>"><?php echo $dname; ?><?php echo $dform; ?>
</option>
<?php
}?>
</select><span id="dcode"></span>
<?php
}
?>
</td>
<td > Drug Form <br>
<input type="text" name="$DrugForm " id="$DrugForm " class="text"
size="25" value="" /><br> <span id="msgbox1"></span><span id="msgbox"
style="display:none"
></span></td>
<td > Quantity in Pack <br>
<input type="text" name="$qty " id="$qty " class="text" size="25" value="" /><br>
<span id="msgbox3"></span><span id="msgbox2" style="display:none" ></span></td>
</form>
根据您提供的解决方案,我能够重新创建代码,不幸的是,我在使用组合时遇到了问题。我只从表中获取最后一条记录,循环了表中的许多记录,我有 5 条测试记录,它循环最后一条记录 4 次。 from 循环 foreach 语句使用的所有子数组变量都会带来错误,如下代码所示是数组生成代码的输出
Array ([ID] => 6 [DrugName] => mm [Strength] => 30 [DrugForm] => ML [Units] => mg [PackType] => Bx )
Array ([ID] => 7 [DrugName] => test [Strength] => 30 [DrugForm] => Box [Units] => mg [PackType] => Bx )
Array ([ID] => 8 [DrugName] => test2 [Strength] => 12 [DrugForm] => ML [Units] => g [PackType] => Bt )
Array ([ID] => 9 [DrugName] => test3 [Strength] => 54 [DrugForm] => ML [Units] => mg [PackType] => Bx)
Array ([ID] => 10 [DrugName] => test4 [Strength] => 54 [DrugForm] => ML [Units] => mg [PackType] => Bx )
Array ([ID] => 11 [DrugName] => test5 [Strength] => 455 DrugForm] => Aerosol [Units] => mg [PackType] => Bx )
<?php
session_start();
// produce array
$mysqli = new mysqli("localhost", "root", "", "drugmgmnt");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if (isset($_GET['DrugName'])) {
$DrugName = $_GET['DrugName'];
} else {
$DrugName = "";
}
if (isset($_GET['DrugForm '])) {
$DrugForm = $_GET['DrugForm '];
} else {
$DrugForm = "";
}
if (isset($_GET['Strength '])) {
$Strength = $_GET['Strength '];
} else {
$Strength = "";
}
if (isset($_GET['Units '])) {
$Units = $_GET['Units '];
} else {
$Units = "";
}
if (isset($_GET['QtyInPack '])) {
$QtyInPack = $_GET['QtyInPack '];
} else {
$QtyInPack = "";
}
if (isset($_GET['ID '])) {
$ID = $_GET['ID '];
} else {
$ID = "";
}
$druglquery = "SELECT ID,DrugName,Strength,DrugForm,Units,PackType FROM DrugsInformation ";
if ($result = $mysqli->query($druglquery)) {
$arr = [];
}
while ($row = $result->fetch_assoc()) {
$arr['ID'] = $row['ID'];
$arr['DrugName'] = $row['DrugName'];
$arr['Strength'] = $row['Strength'];
$arr['DrugForm'] = $row['DrugForm'];
$arr['Units'] = $row['Units'];
$arr['PackType'] = $row['PackType'];
print_r($arr); /* i used this to view the array generated above*/
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<script>
document.addEventListener('DOMContentLoaded',e=>{
let oForm=document.forms.admin;
document.querySelector('select[name="drug"]').addEventListener('change',function(e)
{
let option=this.options[this.options.selectedIndex];
oForm.Strength.value=option.dataset.Strength;
oForm.Units.value=option.dataset.Units;
oForm.DrugForm.value=option.dataset.DrugForm;
oForm.PackType.value=option.dataset.PackType;
});
});
</script>
</head>
<body>
<form name='admin' method='post'>
<table>
<tr>
<td>
<select name='drug'>
<option >Please select your drug
<?php
/* emulate iterating through recordset */
foreach( $arr as $d ){
// when i use subarray to below variables i get undefined
'ID' and the above foreach loops only the last record
printf(
'<option value="%d" data-Strength="%d" data-
Units="%d" data-PackType="%s" data-DrugForm="%s">%s',
$arr['ID'], /* when i use ($d['ID']) i get this
error on all be variables Illegal string offset 'ID*/
$arr['Strength'],
$arr['Units'],
$arr['PackType'],
$arr['DrugForm'],
$arr['DrugName']
);
}
?>
</select>
</td>
<td>
<label>Strength: <input type='text' name='Strength' /></label>
</td>
<td>
<label>Units: <input type='text' name='Units' /></label>
</td>
<td>
<label>DrugForm: <input type='text' name='DrugForm' /></label>
</td>
<td>
<label>PackType: <input type='text' name='PackType' /></label>
</td>
</tr>
</table>
</form>
</body>
</html>