post label value or table cell value or hidden text value in php
This is a simple problem. I am working on a PHP project related to customers and vendors. When I select customerid
from the database, unpaid bills of that customer populate automatically in a table, so table contains a check, the bill number, the bill amount, the paid amount, and a "remove" column.
This remove link will remove that row, so that I can pay only for some bills. (If I have 5 unpaid bills, I could remove 3 bills and enter the data for the other 2 bills). Here I want to show billno
and billamount
as labels, and the payment amount as a textbox for entering the amount. I need to post the billno
to the server because I may remove some rows. But $_POST
of PHP doesn't recognize a label. I tried with a td
id (table cell id) and input type "hidden" but that didn't work. Can you suggest a way to do this?
Here is my code:
Table code:
<TABLE id="mytable1">
<tr>
<th>Select</th>
<th>Bill No</th>
<th>Bill Amount</th>
<th>Received Amount</th>
<th>Remove</th>
</tr>
<TR>
<td ><INPUT type="checkbox" name="chk[]" ></td>
<td ><label id="labelidinv"></label><input type="hidden" name="invhide[]" value="12" ></td>
<TD><label id="labelidinvamt"></label> </TD>
<TD><input name="receivedamt[]" type="text" size="6" id="receivedamt" ></TD>
<td> <a href="#" onClick="deleteRowReceipt('mytable1')">Remove </a&g开发者_运维知识库t; </td>
</TR>
</table>
Ajax code:
var inv_id = xmlDoc.getElementsByTagName("inv_id");
var inv_amt = xmlDoc.getElementsByTagName("inv_amt");
var count = xmlDoc.getElementsByTagName("count");
var inv_id_desc = inv_id[0].firstChild.nodeValue;
var inv_amt_desc = inv_amt[0].firstChild.nodeValue;
var countdesc = count[0].firstChild.nodeValue;
document.getElementById('labelidinv').innerHTML =inv_id_desc;
document.getElementById('labelidinvamt').innerHTML=inv_amt_desc;
var table = document.getElementById('mytable1');
for(var j=1;j<countdesc;j++)
{
var rowCount = table.rows.length;
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[1].cells[i].innerHTML;
if(i==1)
newcell.innerHTML = inv_id[j].firstChild.nodeValue;
if(i==2)
newcell.innerHTML= inv_amt[j].firstChild.nodeValue;
}
}
Here I am not able to post label id values. Is there any way to do it like that? I want the read only values in labels, and I need to post them back to server. Thanks in advance.
EditI am sending ajax request like this
var custname = document.getElementById('customer').options[document.getElementById('customer').selectedIndex].value;
xmlhttp.open("POST","../MODEL/AjaxRequestHandler.php?action=custdetails&CUST_NAME="+custname,true)
At AjaxRequestHandler page:
$CUST_NAME = $_GET['CUST_NAME'];
$querry1 = "select * from RECEIVABLES where CUSTOMER_NAME = $cust_name and FULL_PART_PAID !='F' and voucher_type='SI' ";
/*full_part_paid column is whether that bill fully paid or not, and voucher type = "Sales invoice"*/
$res1 = odbc_exec($conn,$querry1);
$i = 0;
echo '<?xml version="1.0" encoding="utf-8"?>'."\n";
echo '<XmlResponse>'. "\n";
while(odbc_fetch_row($res1))
{
$inv_id= odbc_result($res1, "invoice_id");
echo '<inv_id>'.$inv_id.'</inv_id>'. "\n";
$inv_amt = odbc_result($res1,"invoice_amount");
echo '<inv_amt>'.$inv_amt.'</inv_amt>'. "\n";
$i++;
}
echo '<count>'.$i.'</count>'."\n";
echo '</XmlResponse>'. "\n";
If I am understanding what you are trying to do, then here's my suggestion:
Change
<input name="receivedamt[]" type="text" size="6" id="receivedamt">
to:
<input name="receivedamt_<?= $row['id'] ?>" type="text" size="6" id="receivedamt_<?= $row['id'] ?>">
where $row['id']
is the actual ID of this database row. That way, when the postdata comes in, you will be able to know what rows are paid as follows:
<?php
foreach($_POST as $key => $value) {
$key_parts = explode('_',$key);
if($key_parts[0] == 'receivedamt' && !empty($value) {
$id = intval($key_parts[1]);
$amt = doubleval($value);
mysql_query("UPDATE `some_table` SET `receivedamt`=$amt WHERE `id`=$id");
}
}
That way, you don't even have to worry about the checkboxes or deleting the rows. Just leave any payments you don't want to pay blank, and the server will not update them. Or, delete the rows from the DOM if you prefer, the server will still not update them because they won't be sent in $_POST
.
Does that make sense?
One way to get around this problem is not to delete the rows of paid bills, which is bad practice anyway. Why are you deleting the record of the bill that was paid...what if a problem arises and you need to confirm past payments? Instead, I would add a column to the table for "paid" and change the value from 0 to 1 when a payment goes through. Then you don't have to send the labels back to the server, and can just pull the data for bills with a "paid" value of 0 for display on the pay site.
精彩评论