There are couple of issues.
HTML structure and Form tags are not proper, they should be in body
<html>
<head>
<script>
</script>
</head>
<body>
<form>
</form>
</body>
</html>
this is the structure you should follow
They way you are writing your onChange function is correct, but the issue in my opinion is in the showResult JS function
in the function you have used
<script type="text/javascript">
function showResult()
{
var companyName = companyNameList.value;
var insertvalue = d.getElementById('lblCompanyName');
insertvalue.innerHTML = companyName
// document.getElementById("lblCompanyName").value=document.getElementById("companyNameList").value;
<?php echo "hi";?>
}
</script>
please note that <?PHP echo 'hi'; ?>
will only out put 'hi' in the JS which means nothing and may cause JS errors. you should use it like this
<script type="text/javascript">
function showResult()
{
var companyName = companyNameList.value;
var insertvalue = d.getElementById('lblCompanyName');
insertvalue.innerHTML = companyName
// document.getElementById("lblCompanyName").value=document.getElementById("companyNameList").value;
<?php echo "alert('hi');";?>
}
</script>
I hope these will solve your issues
EDIT
Give an id to your dropdown select companyNameList
.
<script type="text/javascript">
function showResult()
{
<?php echo "alert('hi');";?>
var companyName = document.getElementById("companyNameList");
var companyNameValue = companyName.options[companyName.selectedIndex].value;
var insertvalue = document.getElementById('lblCompanyName');
insertvalue.innerHTML = companyNameValue;
}
</script>
Please install firebug
in your firefox to catch any errors in script
EDIT COMPLETE CODE
Check this code
<?php
$link = mysqli_connect('localhost', 'root', '','xerox_test');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$statement = "SELECT * from form_results";
$result = mysqli_query($link,$statement);
$placement= array();
$i=0;
while($row = mysqli_fetch_array($result))
{
$rowktl[$i] = $row;
$placement[$i]['company_name'] = $rowktl[$i]['company_name'];
$i++;
}
?>
<html>
<head>
<script type="text/javascript">
function showResult()
{
<?php echo "alert('hi');";?>
var companyName = document.getElementById("companyNameList");
var companyNameValue = companyName.options[companyName.selectedIndex].value;
var insertvalue = document.getElementById('lblCompanyName');
alert(companyNameValue);
insertvalue.innerHTML = companyNameValue;
}
</script>
</head>
<body>
<form method="post" action="">
<p>Select the data</p>
<?php
echo '<select name="companyNameList" id="companyNameList" onchange="showResult()" >';
for ($row = 0; $row < 2; $row++)
{
echo '<option value="'.$placement[$row]["company_name"].'">'.$placement[$row]["company_name"].'</option>';
}
echo '</select>';
?>
<br>
<input name="Submit" type="button" class="Submit" value="Submit" onClick="showResult()" />
<table border=2>
<tr>
<td>Case Number <br></td>
<td>Technican ID <br></td>
<td>Company Name <br><label id="lblCompanyName">Here</label> </td>
</tr>
<tr>
<td>Model Number <br></td>
<td>Machine Serial Number<br></td>
<td>Company Address<br></td>
</tr>
<tr>
<td>Customer Name<br><?php echo json_encode($placement[0]['customer_name']);?></td>
<td>Customer Email<br></td>
<td>Contact Number<br></td>
</tr>
<tr><td colspan=3>Remark<br></td></tr>
</table>
</form>
</body>
</html>
I have added the id="companyNameList"
to <select>
in php code above the loop. Also updated the JS function.
Now if you use this, can you see the selected value in alert? If you can not see that it means we are not yet getting the selected value. If you see the proper value, it means the assignment is not correct.