I am trying to make an invoice where in product i have used autocomplete to fetch the product name and price from the database.
It's coming easily for the first row, but for the second row it's not working at all.
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery-ui-1.10.4.custom.min.js" type="text/livescript"></script>
<link rel="stylesheet" href="syle/style.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/main.js"></script>
<title>Untitled Document</title>
<script>
$(document).ready(function(){
var ac_config = {
source: "autosuggest.php",
select: function(event, ui){
$(".menu_name").val(ui.item.menu_name);
$(".menu_code").val(ui.item.menu_code);
$(".menu_price").val(ui.item.menu_price);
},
minLength:1
};
$(".street").autocomplete(ac_config);
});
</script>
</head>
<body>
<form action="action.php" method="POST">
<table>
<tr>
<td>Invoice Number</td>
<td><input type="text" name="inv_no" id="inv_no" /></td>
</tr>
<tr>
<td>Invoice Date </td>
<td><input type="date" name="inv_date" id="inv_date" /></td>
</tr>
</table>
<table border="1" style="border-collapse:collapse">
<thead>
<tr>
<th>No</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Amount</th>
</tr>
</thead>
<tbody id="inv_detail">
<tr>
<th><b class="no">1</b></th>
<td><input type="text" class="street" class="menu_name" name="p_name[]" /></td>
<td><input type="text" name="qty[]" class="qty" /></td>
<td><input type="text" name="price[]" class="menu_price" /></td>
<td align="right"><b class="amt"> </b></td>
</tr>
</tbody>
<tfoot>
<th colspan="6"><b class="total" value="add">0</b></th>
</tfoot>
</table>
</form>
<input type="button" id="click" value="add"/>
</body>
</html>
<script type="text/javascript">
$.datepicker.setDefaults({
changeMonth:true,
changeYear:true,
dateFormat:'yy/mm//dd'
});
$(function(){
$('#click').click(function(){
addnewrow();
});
$('#inv_detail').delegate('.p_name','change',function(){
var p_name = $('.p_name:last').val();
if(p_name !='')
{
addnewrow();
}
if($(this).val()== '' && $('.p_name').length > 1 && p_name == '')
{
$('.p_name:last').parent().parent().remove();
total();
}
});
$('#inv_detail').delegate('.qty,.menu_price',function(){
var tr = $(this).parent().parent();
var qty = tr.find('.qty').val()-0;
var menu_price = tr.find('.menu_price').val()-0;
var amt = qty * menu_price;
tr.find('.amt').html(amt);
total();
});
$('input[type=date]').datepicker();
$('body').delegate('input[type=date]',click,function(e)
{
$(this).datepicker();
});
function addnewrow()
{
var n = ($('#inv_detail tr').length-0)+1;
var row ='<tr>'+
'<th><b class="no">'+ n +'</b></th>'+
'<td><input type="text" name="p_name[]" class="menu_name"/></td>'+
'<td><input type="text" name="qty[]" class="qty"/></td>'+
'<td><input type="text" name="price[]" class="menu_price"/></td>'+
'<td allign="right"><b class="amt"></b></td>'+
'</tr>';
$('#inv_detail').append(row);
}
});
function total(){
var gg=0;
$('.amt').each(function(i,el){
var amt = $(this).html()-0;
gg += amt;
});
$('.total').html(gg);
}
</script>
action.php
<?php
$cn = mysql_connect('localhost','root','');
mysql_select_db('phpinvoice',$cn);
//if(isset($_post['submit']))
//{
echo $sql="INSERT INTO inv (inv_no,inv_date)VALUES('{$_POST['inv_no']}','{$_POST['inv_date']}')";
$query=mysql_query($sql);
$id = mysql_insert_id();
$_p_name = $_POST['p_name'];
$_qty = $_POST['qty'];
$_price = $_POST['price'];
for($i = 0;$i < count($_p_name);$i++)
{
echo $sql1="INSERT INTO inv_detail SET
inv_id='{$id}',
p_name='$_p_name[$i]',
qty='$_qty[$i]',
price='$_price[$i]',
";
$qyer=mysql_query($sql1);
header('Location:http://www.pingbd.com');
}
//}
?>