พี่ๆ ครับ ช่วยเรื่องการคำนวณใน textbox ผ่าน javascript ด้วยครับ หรือผมทำผิดตรงจุดไหนบ้างครับ
getMain.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script language="JavaScript">
function fncSum2()
{
if(isNaN(document.frmMain.ProductCount.value) || document.frmMain.ProductCount.value == "")
{
alert('.........');
document.frmMain.ProductCount.focus();
return;
}
document.frmMain.ProductSumPrice.value = parseFloat(document.frmMain.ProductCount.value) * parseFloat(document.frmMain.ProductPrice.value);
}
</script>
<script language="javascript">
function OpenPopup(intLine)
{
window.open('getData.php?Line='+intLine,'myPopup','width=650,height=200,toolbar=0, menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');
}
function CreateNewRow()
{
var intLine = parseInt(document.frmMain.hdnMaxLine.value);
intLine++;
var theTable = document.getElementById("tbExp");
var newRow = theTable.insertRow(theTable.rows.length)
newRow.id = newRow.uniqueID
var newCell
//*** Column Popup ***//
newCell = newRow.insertCell(0);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
//newCell.setAttribute("OnClick", "OpenPopup('"+intLine+"')");
newCell.innerHTML = "<center><INPUT TYPE=\"BUTTON\" NAME=\"btnPopup_"+intLine+"\" ID=\"btnPopup_"+intLine+"\" VALUE=\"...\" OnClick=\"OpenPopup('"+intLine+"')\"></center>";
//*** Column No ***//
newCell = newRow.insertCell(1);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center>"+intLine+"</center>";
//*** Column ProductID ***//
newCell = newRow.insertCell(2);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"ProductID_"+intLine+"\" ID=\"ProductID_"+intLine+"\" VALUE=\"\"></center>";
//*** Column ProductName ***//
newCell = newRow.insertCell(3);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"ProductName_"+intLine+"\" ID=\"ProductName_"+intLine+"\" VALUE=\"\"></center>";
//*** Column ProductPrice ***//
newCell = newRow.insertCell(4);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"ProductPrice_"+intLine+"\" ID=\"ProductPrice_"+intLine+"\" VALUE=\"\"></center>";
//*** Column ProductCount ***//
newCell = newRow.insertCell(5);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"ProductCount_"+intLine+"\" ID=\"ProductCount_"+intLine+"\" VALUE=\"\" OnChange=\"fncSum2()\"></center>";
//*** Create Option ***//
// CreateSelectOption("txtCountryCode_"+intLine)
//*** Column ProductSumPrice ***//
newCell = newRow.insertCell(6);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"ProductSumPrice_"+intLine+"\" ID=\"ProductSumPrice_"+intLine+"\" VALUE=\"\"></center>";
//*** Column Used ***//
// newCell = newRow.insertCell(7);
// newCell.id = newCell.uniqueID;
// newCell.setAttribute("className", "css-name");
// newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"txtUsed_"+intLine+"\" ID=\"txtUsed_"+intLine+"\" VALUE=\"\"></center>";
//*** Column 7 ***//
document.frmMain.hdnMaxLine.value = intLine;
}
function RemoveRow()
{
intLine = parseInt(document.frmMain.hdnMaxLine.value);
if(parseInt(intLine) > 0)
{
theTable = document.getElementById("tbExp");
theTableBody = theTable.tBodies[0];
theTableBody.deleteRow(intLine);
intLine--;
document.frmMain.hdnMaxLine.value = intLine;
}
}
function GenerateRow()
{
var intRows = parseInt(document.frmMain.txtCount.value);
for(i=0;i<intRows;i++)
{
CreateNewRow();
}
}
</script>
<!-- ถ้าต้องการให้แถวแรกขึ้น ให้ใส่ OnLoad="CreateNewRow();" ใน tag body-->
<body>
<form name="frmMain" method="post" action="readData.php">
<table width="600" border="1" id="tbExp">
<tr>
<td><div align="center">Popup </div></td>
<td><div align="center"># </div></td>
<td><div align="center">รหัสสินค้า </div></td>
<td><div align="center">ชื่อสินค้า </div></td>
<td><div align="center">ราคา / ชิ้น </div></td>
<td><div align="center">จำนวน </div></td>
<td><div align="center">รวมราคา </div></td>
<!-- <td><div align="center">ลบ </div></td> -->
</tr>
</table>
<input type="hidden" name="hdnMaxLine" value="0">
<input type="text" name="txtCount" value="1" size="5">
<!-- ถ้าต้องการให้เพิ่มได้ทีละ หลายๆ แถว ให้เปลี่ยนที่ onClick="CreateNewRow();" เป็น onClick="GenerateRow();"-->
<input name="btnAdd" type="button" id="btnAdd" value="+" onClick="GenerateRow();">
<input name="btnDel" type="button" id="btnDel" value="-" onClick="RemoveRow();">
<input type="submit" name="btnSubmit" value="Submit">
</form>
</body>
</html>
getData.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script language="javascript">
function selData(intLine,ProductID,ProductName,ProductPrice)
{
var sProductID = self.opener.document.getElementById("ProductID_" +intLine);
sProductID.value = ProductID;
var sProductName = self.opener.document.getElementById("ProductName_" +intLine);
sProductName.value = ProductName;
var sProductPrice = self.opener.document.getElementById("ProductPrice_" +intLine);
sProductPrice.value = ProductPrice;
// var sProductCount = self.opener.document.getElementById("ProductCount_" +intLine);
// sProductCount.value = ProductCount;
// var sProductSumPrice = self.opener.document.getElementById("ProductSumPrice_" +intLine);
// sProductSumPrice.value = ProductSumPrice;
// var sUsed = self.opener.document.getElementById("txtUsed_" +intLine);
// sUsed.value = Used;
window.close();
}
</script>
<body>
<?
$objConnect = mysql_connect("localhost","root","sol#254") or die("Error Connect to Database");
$objDB = mysql_select_db("db_cooperative");
$strSQL = "SELECT * FROM tb_product";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<!-- <th width="71"> <div align="center">Used </div></th> -->
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><a href="#" OnClick="selData('<?=$_GET["Line"];?>' ,'<?=$objResult["Pro_ID"];?>', '<?=$objResult["Pro_Name"];?>','<?=$objResult["Pro_Price"];?>');">
<?=$objResult["Pro_ID"];?>
</a></div></td>
<td><?=$objResult["Pro_Name"];?></td>
<td><?=$objResult["Pro_Price"];?></td>
<td><div align="center"><?=$objResult["Pro_Stock"];?></div></td>
<!-- <td align="right"><?=$objResult["Order_Point"];?></td> -->
<!-- <td align="right"><?=$objResult["Used"];?></td> -->
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
</body>
</html>
Tag : PHP, MySQL, JavaScript, jQuery
Date :
2014-07-05 01:06:54
By :
san.saleah
View :
1015
Reply :
28
Load balance : Server 01