i have a combo box which is collecting fields from table and i would like to use the selected id to display other values in different text boxes. get Element By Id only returns a single field.
<?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>
using the same id i would like to have something like below
<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>
Looking through the modified version of the answer I previously provided I believe you were trying to do something like this. In the previous example I only used a static array so that I could emulate/mimic a typical recordset rather than as the solution itself which, after all, really was the Javascript!
However, in your modified code you assigned values from the recordset to the $arr
variable but on each iteration through the recordset you simply overwrote the previous values which meant at the end of the loop there would have been a single dimension in the array.
That stage is unnecessary when you are processing the recordset ( remember I only used the array to emulate the recordset ) - iterate through the $results
and generate the menu directly as before ( for convenience and clarity I like 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>