<script language="javascript" >
function OperasiPerhitungan(){
A1 = document.getElementById("angka1").value;
A2 = document.getElementById("angka2").value;
A3 = (parseFloat(A1) + parseFloat(A2))/2;
document.getElementById("hasil").value =A3;
}
</script>
<div class="col-sm-9 col-sm-offset-3 col-lg-10 col-lg-offset-2 main">
<div class="row">
<div class="col-lg-12">
<center><h3 class="page-header">EDIT NILAI</h3></center>
</div>
</div><!--/.row-->
<?php include ('koneksi.php');
$nip=$_SESSION['$nip'];
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-danger">
<div class="panel-body">
<form method="POST" action="input_nilai_edit_proses.php">
<table id="example" class="" cellspacing="0" width="100%">
<thead>
<tr class="alert warning">
<th>No</th>
<th></th>
<th>NIS</th>
<th>Siswa</th>
<th>UH 1</th>
<th>UH 2</th>
<th>RERATA</th>
</tr>
</thead>
<tbody>
<?php
include('koneksi.php');
$id_mapel = $_GET['id_mapel'];
$query = mysql_query("SELECT siswa.nama,nilai.id_mapel, nilai.id_nilai,
nilai.nis, nilai.uh1,nilai.uh2
FROM siswa, nilai WHERE siswa.nis=nilai.nis AND nilai.id_mapel= '$id_mapel'");
$no = 0; //membuat variabel $no untuk membuat nomor urut
while($data = mysql_fetch_array($query)){
$no++;
$id_nilai = $data['id_nilai'];
$id_mapel = $data['id_mapel'];
$rerata = $data['rerata'];
?>
<tr>
<td><?php echo $no; ?></td>
<td><input type="hidden" name="id_nilai[]" value="<?php echo $data['id_nilai']; ?>"></td>
<td><?php echo $data['nis']; ?></td>
<td><?php echo $data['nama']; ?></td>
<td><input type="number" name="uh1[]" id="angka1" onchange="OperasiPerhitungan();" value="0" style="width:50px"></td>
<td><input type="number" name="uh2[]" id="angka2" onchange="OperasiPerhitungan();" value="0" style="width:50px"></td>
<td><input type="number" name="rerata[]" id="hasil" disabled="disabled" value="0" style="width:80px"></td>
</tr>
<?php } ?>
</tbody>
</table>
<div class='navbar-form pull-left'>
<button type="submit" name="tambah" class="btn btn-danger">Simpan</button>
</div>
</form>
itu dikarenakan id dari elementnya hanya 1, selalu hasil. ID dari elemen html harus unik. Pertama ubah bagian tablenya untuk ID-nya jadi unik seperti:
<tr>
<td><?php echo $no; ?></td>
<td><input type="hidden" name="id_nilai[]" value="<?php echo $data['id_nilai']; ?>"></td>
<td><?php echo $data['nis']; ?></td>
<td><?php echo $data['nama']; ?></td>
<td><input type="number" name="uh1[]" id="angka1-<?php echo $no; ?>" onchange="OperasiPerhitungan(<?php echo $no; ?>);" value="0" style="width:50px"></td>
<td><input type="number" name="uh2[]" id="angka2-<?php echo $no; ?>" onchange="OperasiPerhitungan(<?php echo $no; ?>);" value="0" style="width:50px"></td>
<td><input type="number" name="rerata[]" id="hasil-<?php echo $no; ?>" disabled="disabled" value="0" style="width:80px"></td>
</tr>
Kemudian ganti fungsi javascript OperasiPerhitungan
menjadi:
<script language="javascript" >
function OperasiPerhitungan(nomor){
let A1 = document.getElementById("angka1-" + nomor).value;
let A2 = document.getElementById("angka2-" + nomor).value;
let A3 = (parseFloat(A1) + parseFloat(A2)) / 2;
document.getElementById("hasil=" + nomor).value = A3;
}
</script>