newphpcoder Posted April 30, 2012 Share Posted April 30, 2012 Hi.. I have problem in getting the DemandedQty that was input. Only in the Demanded Qty of P28 items was read. For example I input 21 in Demanded Qty of P28 and then It alerts 21, but when I input 2 in Demanded Qty of P30 the alert was 21. It means only the P28 Demanded Qty was get. here is my code: <?php error_reporting(0); date_default_timezone_set("Asia/Singapore"); //set the time zone $con = mysql_connect('localhost', 'root',''); if (!$con) { echo 'failed'; die(); } mysql_select_db("mes", $con); ?> <html> <title>Stock Requisition</title> <head> <link rel="stylesheet" type="text/css" href="kanban.css"> <script type="text/javascript"> function showSum(element) { var clickElement = element.value; var click_id = element.id; var Table = document.getElementById('list'); var rows = Table.rows; var strSelect = document.getElementById(click_id).value; alert(strSelect); // i tried to alert to check if he gets the correct value, but I found that only the Demanded Qty of P28 was read. And when I try to input on another Demanded Qty like in P30 the alert was blank. for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (row.id.substr(0,3) == strSelect) { } } } </script> </head> <body> <form name="stock_requisition" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div> <table id="list"> <thead> <th>Items</th> <th>Sub Items</th> <th>Item Code</th> <th>Demanded Qty</th> <th>UoM</th> <th>Class</th> <th>Description</th> <th>BIN Location</th> </thead> <?php $DemandedQty = $_POST['DemandedQty']; $sql = "SELECT DISTINCT Items FROM bom_subitems ORDER BY Items"; $res_bom = mysql_query($sql, $con); $rowCounter = 1; while($row = mysql_fetch_assoc($res_bom)){ $Items = $row['Items']; $Items_ = substr($Items, 12, 3); echo "<tr> <td style='border: none;font-weight: bold;'> <input type='name' value='$Items_' name='Items_[]' id='Items_[$rowCounter]' readonly = 'readonly' style = 'border:none;width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border:none;'> </td> <td style='border:none;'> </td> <td style='border: none;'><center><input type='text' style='text-align: right;' name='DemandedQty[]' id='DemandedQty' value='' size='12' onkeyup ='showSum(this);'></center></td> </tr>"; $sql = "SELECT Items, SubItems, ItemCode, UoM, Class, Description, BINLocation, Quantity FROM bom_subitems WHERE Items = '$Items' ORDER BY Items"or die(mysql_error()); $res_sub = mysql_query($sql, $con); $rowCounter = 1; while($row_sub = mysql_fetch_assoc($res_sub)){ $Items = $row_sub['Items']; //$Items_ = substr($Items, 12, 3); $SubItems = $row_sub['SubItems']; $ItemCode = $row_sub['ItemCode']; $UoM = $row_sub['UoM']; $Class = $row_sub['Class']; $Description = $row_sub['Description']; $BINLocation = $row_sub['BINLocation']; $Quantity = $row_sub['Quantity']; echo "<input type='hidden' name='Quantity' id='Quantity' value='$Quantity'>"; echo "<tr> <td style='border: none;'> <input type='hidden' value='$Items' id='Items[$rowCounter]' name='Items[]'></td> <td style='border: none;'> <input type='text' name='SubItems[]' value='$SubItems' id='SubItems' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border: none;'> <input type='text' name='ItemCode[]' value='$ItemCode' id='ItemCode' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border: none;'><center><input type='text' name='SubQty[]' id='SubQty[$rowCounter]' value='' size='12' style='border:none;text-align: right;'></center></td> <td style='border: none;' size='3'> <input type='text' name='UoM[]' value='$UoM' id='UoM' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;' size='3'></td> <td style='border: none;'> <input type='text' name='Class[]' value='$Class' id='Class' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border: none;'> <input type='text' name='Description[]' value='$Description' id='Description' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;' size= '30'></td> <td style='border: none;'> <input type='text' name='BINLocation[]' value='$BINLocation' id='BINLocation' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> </tr>"; $rowCounter = $rowCounter + 1; } $rowCounter = $rowCounter + 1; } ?> </table> </div> </form> </body> </html> Thank you so much Link to comment https://forums.phpfreaks.com/topic/261831-problem-in-counting-row-to-get-the-correct-qty-per-item/ Share on other sites More sharing options...
Kalland Posted April 30, 2012 Share Posted April 30, 2012 Hi, I think that is because you're giving the DemandedQty text fields the same ID. Try giving them a unique ID. Link to comment https://forums.phpfreaks.com/topic/261831-problem-in-counting-row-to-get-the-correct-qty-per-item/#findComment-1341642 Share on other sites More sharing options...
newphpcoder Posted April 30, 2012 Author Share Posted April 30, 2012 Hi... it works now.. Using this code: <?php error_reporting(0); date_default_timezone_set("Asia/Singapore"); //set the time zone $con = mysql_connect('localhost', 'root',''); if (!$con) { echo 'failed'; die(); } mysql_select_db("mes", $con); ?> <html> <title>Stock Requisition</title> <head> <link rel="stylesheet" type="text/css" href="kanban.css"> <script type="text/javascript"> function showSum(element) { var clickElement = element.value; var click_id = element.id; var quantityElement = element.value; var quantity_id = element.id; var subqtyElement = element.value; var subqty_id = element.id; var Table = document.getElementById('list'); var rows = Table.rows; var strSelect = document.getElementById(click_id).value; var Quantity = document.getElementById(quantity_id).value; //alert(Quantity); var SubQty = document.getElementById(subqty_id).value; for (var i = 0; i < rows.length; i++) { var row = rows[i]; // if (row.id.substr(0,3) == strSelect) { if (strSelect.value != "") { SubQty.value = strSelect.value * Quantity.value; //alert(SubQty.value); //alert(Items_.value); //} // row.style.display = ((document.getElementById(click_id).checked) == false) ? 'none' : '' // alert('test'); } } } </script> </head> <body> <form name="stock_requisition" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div> <table id="list"> <thead> <th>Items</th> <th>Sub Items</th> <th>Item Code</th> <th>Demanded Qty</th> <th>UoM</th> <th>Class</th> <th>Description</th> <th>BIN Location</th> </thead> <?php $DemandedQty = $_POST['DemandedQty']; $sql = "SELECT DISTINCT Items FROM bom_subitems ORDER BY Items"; $res_bom = mysql_query($sql, $con); while($row = mysql_fetch_assoc($res_bom)){ $Items = $row['Items']; $Items_ = substr($Items, 12, 3); echo "<tr> <td style='border: none;font-weight: bold;'> <input type='name' value='$Items_' name='Items_[]' id='Items_[$rowCounter]' readonly = 'readonly' style = 'border:none;width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border:none;'> </td> <td style='border:none;'> </td> <td style='border: none;'><center><input type='text' style='text-align: right;' name='DemandedQty[]' id='DemandedQty[$rowCounter]' value='' size='12' onkeyup ='showSum(this);'></center></td> </tr>"; $sql = "SELECT Items, SubItems, ItemCode, UoM, Class, Description, BINLocation, Quantity FROM bom_subitems WHERE Items = '$Items' ORDER BY Items"or die(mysql_error()); $res_sub = mysql_query($sql, $con); $rowCounter = 1; while($row_sub = mysql_fetch_assoc($res_sub)){ $Items = $row_sub['Items']; $SubItems = $row_sub['SubItems']; $ItemCode = $row_sub['ItemCode']; $UoM = $row_sub['UoM']; $Class = $row_sub['Class']; $Description = $row_sub['Description']; $BINLocation = $row_sub['BINLocation']; $Quantity = $row_sub['Quantity']; echo "<input type='hidden' name='Quantity[]' id='Quantity[$rowCounter]' value='$Quantity'>"; echo "<tr> <td style='border: none;'> <input type='hidden' value='$Items' id='Items[$rowCounter]' name='Items[]'></td> <td style='border: none;'> <input type='text' name='SubItems[]' value='$SubItems' id='SubItems' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border: none;'> <input type='text' name='ItemCode[]' value='$ItemCode' id='ItemCode' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border: none;'><center><input type='text' name='SubQty[]' id='SubQty[$rowCounter]' value='' size='12' style='border:none;text-align: right;'></center></td> <td style='border: none;' size='3'> <input type='text' name='UoM[]' value='$UoM' id='UoM' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;' size='3'></td> <td style='border: none;'> <input type='text' name='Class[]' value='$Class' id='Class' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border: none;'> <input type='text' name='Description[]' value='$Description' id='Description' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;' size= '30'></td> <td style='border: none;'> <input type='text' name='BINLocation[]' value='$BINLocation' id='BINLocation' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> </tr>"; $rowCounter = $rowCounter + 1; } $rowCounter = $rowCounter + 1; } ?> </table> </div> </form> </body> </html> Niow I tried to compute the subqty of all subitems per Items.Once I input Demanded Qty per item. yet it did not work. no data displayed in SubQty Thank you Link to comment https://forums.phpfreaks.com/topic/261831-problem-in-counting-row-to-get-the-correct-qty-per-item/#findComment-1341643 Share on other sites More sharing options...
Kalland Posted May 1, 2012 Share Posted May 1, 2012 That is because you are not referring to the element SubQty, but the value of the element instead. The following line: var SubQty = document.getElementById(subqty_id).value; Should be changed into the following: var SubQty = document.getElementById(subqty_id); And the if-statement following should be like this: if (strSelect != "") { SubQty.value = strSelect * Quantity; } Hope this was helpful. Link to comment https://forums.phpfreaks.com/topic/261831-problem-in-counting-row-to-get-the-correct-qty-per-item/#findComment-1342036 Share on other sites More sharing options...
newphpcoder Posted May 2, 2012 Author Share Posted May 2, 2012 Hi.. i tried your suggestion: <script type="text/javascript"> function showSum(element) { var clickElement = element.value; var click_id = element.id; var quantityElement = element.value; var quantity_id = element.id; var subqtyElement = element.value; var subqty_id = element.id; var Table = document.getElementById('list'); var rows = Table.rows; var strSelect = document.getElementById(click_id).value; var Quantity = document.getElementById(quantity_id).value; var SubQty = document.getElementById(subqty_id); for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (strSelect != "") { SubQty.value = strSelect * Quantity; } } } </script> and the output is: when I type 1 then 0 the Demanded become 100:( Thank you Link to comment https://forums.phpfreaks.com/topic/261831-problem-in-counting-row-to-get-the-correct-qty-per-item/#findComment-1342168 Share on other sites More sharing options...
Kalland Posted May 2, 2012 Share Posted May 2, 2012 My mistake for not noticing it earlier. If I understand you correctly, you want to calculate a sub sum for every item? The variables you define in the beginning of your function will only hold info from the DemandedQty text field you're currently editing. This results in only changing the DemandedQty and not the SubQty that you want to change. Find a way to reference the id's of the text fields SubQty{i} and Quantity{i}. <script type="text/javascript"> // Here you are passing in a reference to an element. // This function is, from what I can see, run when the user is editing any of the DemandedQty text fields function showSum (element) { var clickElement = element.value; // This will contain a reference to the DemandedQty elements value var click_id = element.id; // This will contain a reference to the DemandedQty elements id var quantityElement = element.value; // This will also contain a reference to the DemandedQty elements value var quantity_id = element.id; // This will also contain a reference to the DemandedQty elements id var subqtyElement = element.value; // This will also contain a reference to the DemandedQty elements value var subqty_id = element.id; // This will also contain a reference to the DemandedQty elements id var Table = document.getElementById('list'); var rows = Table.rows; var strSelect = document.getElementById(click_id).value; // This will contain the value of DemandedQty var Quantity = document.getElementById(quantity_id).value; // This will contain the value of DemandedQty var SubQty = document.getElementById(subqty_id); // This will contain a reference to DemandedQty for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (strSelect != "") { SubQty.value = strSelect * Quantity; // So this will only change the value of DemandedQty you're editing } } } </script> Here I have put together a very simple working example: <!DOCTYPE html> <html> <head> <script> /** Calculate sub sum of item */ function showSum (element) { var qty = element; var elmNumber = qty.id.substring(3); var price = document.getElementById('price' + elmNumber); var sum = document.getElementById('sum' + elmNumber); sum.value = price.value * qty.value; } </script> </head> <body> <table> <tr> <td>Item</td> <td>Quantity</td> <td>Price</td> <td>Sum</td> </tr> <?php for ($i = 0; $i < 10; $i++) : ?> <tr> <td>Item <?php echo $i; ?></td> <td><input name="qty<?php echo $i; ?>" id="qty<?php echo $i; ?>" onkeyup="showSum(this);" /></td> <td><input name="price<?php echo $i; ?>" id="price<?php echo $i; ?>" value="10" /></td> <td><input name="sum<?php echo $i; ?>" id="sum<?php echo $i; ?>" /></td> </tr> <?php endfor; ?> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/261831-problem-in-counting-row-to-get-the-correct-qty-per-item/#findComment-1342231 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.