i have a dynamic table that generates rows when end-user presses a button. I had no problem using it on input boxes. Now im trying to change one input into a combo box that queries data from my db. The problem i have know is how to dynamically add the combo box along with its php code.
$(document).ready(function(){
var counter = 2;
$('.add-row').click(function() {
$(".item_form").append(
'<tr><td><input type="text" name="serialnoa[]" placeholder="serial no.' +
counter + '"/></td></tr>'
);
counter++;
});
$('.del-row').click(function() {
if($(".item_form tr").length != 2)
{
$(".item_form tr:last-child").remove();
counter--;
}
else
{
alert("You cannot delete first row");
}
});
});
<!DOCTYPE html>
<html class="no-js" lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="_css/inventory.css?v=<?=time();?>">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<table class="item_form">
<tr>
<th>serial no.</th>
<th>brand<th>
</tr>
<tr>
<td><input type="text" placeholder="serial no.1" name="serialnoa[]"></td>
<td>
<select name="show_brands[]">
<?php
mysql_connect('localhost', 'root', 'adminpass');
mysql_select_db('my_db');
$sql = "SELECT DISTINCT brand FROM warehouse ORDER BY brand";
$result = mysql_query($sql);
while ($brand=mysql_fetch_assoc($result)) {
echo "<option value='".$brand['brand']."'>".$brand['brand']."</option>";
}
?>
</select>
</td>
</tr>
</table>
<table>
<tr>
<td><a href="#" class="add-row"><div>+ Row</div></td>
<td><a href="#" class="del-row"><div>- Row</div></td>
</tr>
</table>
</body>
</html>