I have created a table which consist of two columns id and name .Then I created rows dynmacially.What I want to do is to store data into db.But I am getting "Undefined offset"error inside for loop
<?php
$con = mysqli_connect('localhost', 'root', '', 'testing');
$count = $_POST['txt'];
for ($i = 0; $i < $count; ++$i) {
$query = "insert into emps (name) values('".$_POST['txt'][$i]."')";
mysqli_query($con, $query);
}
echo 'ok';
What I have tried:
<form id="frm" name="frm">
<table id="tbl">
<tr><td><input type="text" name="txt[]"><button type="button" id="btn">Add</button> </td>
</tr> u
</table>
<button type="button" id="save"></button>
</form>
</body>
</html>
<script>
$(function(){
var i=1;
$('#btn').click(function(){
i++;
$('#tbl').append('<tr id="row'+i+'"><td><input type="text" name="txt[]"><button type="button" id="'+i+'" class="remove">x</button> </td>')
})
$(document).on('click','.remove',function(){
var id=$(this).attr('id');
$('#row'+id+'').css('background-color','#ccc');
$('#row'+id+'').fadeOut('slow');
})
$('#save').click(function(){
$.ajax({
url:"ajax.php",
method:"post",
data:$("#frm").serialize(),
success:function(data){
alert(data);
$('#frm')[0].reset();