I have a purchase bill form which i add row by javascript and retrieve values from mysql database by php.
function addRow(dataTable) {
var table = document.getElementById("dataTable");
var rowCount = table.rows.length;
if(rowCount < 100) {
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
table.appendChild(newrow);
} else {
alert("En fazla 100 satır ekleyebilirsiniz.");
}
var selects = newrow.getElementsByTagName('select');
for(var i=1; i<selects.length;i++) {
selects[i].id = Math.random().toString(36).slice(2);
}
}
function fetch_select(val) {
$.ajax({
type: 'post',
url: 'stoklari_cek.php',
data: {sto_altgrup_kod:val},
success: function (response) {
document.getElementById("kodlar").innerHTML=response;
}
});
}
In the first row when i change first select value i can get the second secelt values from database. But when i add new row and change the first select value, it changes the first rows second select. I tried to give unique ID for each select elements with this code but i could not call it back on the second part of codes. Shortly i want to change second select option values when i change the first select value in each row i added.
This is first select element of every row.
<select style="margin: 0 !important; padding: 0 !important; width:100%" name="gruplar" id="gruplar" class="gruplar" onChange="fetch_select(this.value)" required>
<option value="">--Alt Gruplar--</option>
<?php $altgrup_al = mysql_query("SELECT * FROM stok_alt_gruplari ORDER BY sta_isim ASC");
while($altgrup_cek = mysql_fetch_array($altgrup_al)){
$sta_isim = $altgrup_cek['sta_isim'];
$sta_RECno = $altgrup_cek['sta_RECno'];
echo '<option ';
echo 'value="'.$altgrup_cek['sta_RECno'].'">'.$altgrup_cek['sta_isim'].'</option>';}?>
</select>
And this is the second select that values should change according to the first select above.
<select style="margin: 0 !important; padding: 0 !important; width:100%" class="kodlar" id="kodlar" name="sth_stok_kod[]" required>
<option value="">--Ürünler--</option>