|
|
|
Dynamic Textbox บันทึกข้อมูล php ลง mysql แบบหลายแถวไม่ได้ค่ะ... |
|
|
|
|
|
|
|
script.php
<script language="javascript">
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 No ***//
newCell = newRow.insertCell(0);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center>"+intLine+"</center>";
//*** Column type_id ***//
newCell = newRow.insertCell(1);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
//newCell.setAttribute("OnClick", "OpenPopup('"+intLine+"')");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" NAME=\"type_id[]\" SIZE=\"5\" ID=\"type_id"+intLine+"\" VALUE=\"...\" OnClick=\"OpenPopup('"+intLine+"')\"></center>";
//*** Column type_name ***//
newCell = newRow.insertCell(2);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" NAME=\"type_name[]\" ID=\"type_name"+intLine+"\" VALUE=\"\"></center>";
//*** Column color_name ***//
newCell = newRow.insertCell(3);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
//newCell.setAttribute("OnClick", "OpenPopup('"+intLine+"')");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" NAME=\"color_name[]\" ID=\"color_name"+intLine+"\" VALUE=\"...\" OnClick=\"OpenPopup2('"+intLine+"')\"></center>";
//*** Column unit ***//
newCell = newRow.insertCell(4);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" NAME=\"unit[]\" ID=\"unit"+intLine+"\" VALUE=\"คัน\"></center>"
//*** Column amount ***//
newCell = newRow.insertCell(5);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" NAME=\"amount[]\" ID=\"amount"+intLine+"\" VALUE=\"\" OnChange=\"chkNum(this);textsum("+intLine+");\" onfocus=\"textsum("+intLine+");\" onblur=\"textsum("+intLine+");\" onkeyup=\"textsum("+intLine+");\"></center>";
//*** Column price ***//
newCell = newRow.insertCell(6);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" NAME=\"price[]\" ID=\"price"+intLine+"\" VALUE=\"\" OnChange=\"chkNum(this);textsum("+intLine+");\" onfocus=\"textsum("+intLine+");\" onblur=\"textsum("+intLine+");\" onkeyup=\"textsum("+intLine+");\" ></center>";
document.frmMain.hdnMaxLine.value = intLine;
//*** Column total ***//
newCell = newRow.insertCell(7);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" NAME=\"total[]\" ID=\"total"+intLine+"\" VALUE=\"\" OnChange=\"chkNum(this);textsum("+intLine+");\" onfocus=\"textsum("+intLine+");\" onblur=\"textsum("+intLine+");\" onkeyup=\"textsum("+intLine+");\"></center>";
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;
}
}
</script>
purchase.php
<? session_start(); ?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="main_style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.css">
<title>บันทึกใบสั่งซื้อรถ</title>
</head>
</html>
<? include "script.php";?>
<? include "5.php";?>
<?
include"connect.php";
$strSQL = "SELECT * FROM user u ,prefix p WHERE u.user_id = '".$_SESSION['user_id']."' AND u.prefix = p.prefix_id";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$fullname = $objResult['prefix_name'].$objResult['fname']." ".$objResult['lname'];
?>
<table width="778px" height="40" align="center" style="font-size:13px">
<tr>
<td>
<div align="left" style="font-size:13px;"><img src="image/Motorcycle.png" width="45" height="45" /><b>บันทึกใบสั่งซื้อรถ</b></div>
<hr />
</td></tr>
</table>
<body OnLoad="CreateNewRow()">
<form name="frmMain" method="post" action="save_purchase_bill.php">
<fieldset style="font-size:14px; width:800px; border-color:#666; border-width:1px; ">
<br /><br />
<table width="803" height="40" align="center" style="font-size:13px">
<tr>
<td width="9%" align="right"><b><u>สาขา :</u></b></td>
<td width="49%">
<input type="text" name="branch_no_1" id="branch_no_1" size="5" VALUE="" OnClick="OpenPop(1)" />
<input type="TEXT" name="branch_name_1" id="branch_name_1" size="40" value="" />
</td>
<td align="right" width="15%"><b><u>เลขที่ใบรับสินค้า :</u></b></td>
<td width="27%" align="left">
<input type="text" name="po_id" id="po_id" value="<? echo $PO.'-'. $strNextSeq;?>"class="tbl_box7" size="22"/>
</td>
</tr>
<tr>
<td width="9%" align="right"><b><u>แผนก :</u></b></td>
<td width="49%">
<input type="text" name="type_no_1" id="type_no_1" size="5" VALUE="" OnClick="OpenUp(1)" />
<input type="TEXT" name="type_name_1" id="type_name_1" size="40" value="" />
</td>
<td align="right" width="15%"><b><u>วันที่รับรถ :</u></b></td>
<td align="left">
<input name="date_receive" type="text" class="tbl_box1" id="date_receive" value="<? echo date("d/m/Y");?>" size="25" />
</td>
</tr>
<tr>
<td width="9%" align="right"><b><u>ผู้บันทึก :</u></b></td>
<td width="49%">
<input name="receive_name" class="tbl_box8" id="receive_name" value="<? echo $fullname;?>" size="53"/>
<input type="hidden" name="receive_name" id="receive_name" value="<?=$fullname; ?>" />
</td>
<td align="right"><b><u>วันที่ส่ง :</u></b></td>
<td><input name="date_send" type="text" class="tbl_box1" id="date_send" value="<? echo date("d/m/Y");?>" size="25" /></td>
</tr>
</table>
</fieldset>
<br />
<br />
<!--
<div align="right">
<a href="javascript:window.close()"><img src="image/exit.png" width="40" height="40" border="0" />
<font color="#003300" size="2"><b>ปิดหน้าต่าง</b></font>
</a>
</div>
-->
<table height="78" border="1" id="tbExp" cellpadding="0" cellspacing="0" style="font-size:13px; ">
<tr>
<th width="47"><div align="center">ลำดับ </div></th>
<th width="39"><div align="center">รหัส </div></th>
<th width="82"><div align="center">รายละเอียด </div></th>
<th width="81"><div align="center">สี </div></th>
<th width="64"><div align="center">หน่วย </div></th>
<th width="40"><div align="center">จำนวน</div></th>
<th width="97"><div align="center">ราคา:หน่วย (บาท)</div></th>
<th width="195"><div align="center">รวมเป็นเงิน (บาท)</div></th>
</tr>
</table>
<input type="hidden" name="hdnMaxLine" value="0" >
<input name="btnAdd" type="button" id="btnAdd" value="+" onClick="CreateNewRow();">
<input name="btnDel" type="button" id="btnDel" value="-" onClick="RemoveRow();">
<input type="submit" name="btnSubmit" value="บันทึกข้อมูล" />
</form>
<script language="javascript">
CreateNewRow();
CreateNewRow();
CreateNewRow();
CreateNewRow();
CreateNewRow();
</script>
</body>
</html>
save.php
<body>
<?
for($i=1;$i<=(int)$_POST["hdnMaxLine"];$i++)
{
echo $_POST["type_id[]".$i];
echo "<br>";
echo $_POST["type_name[]".$i];
echo "<br>";
echo $_POST["color_name[]".$i];
echo "<br>";
}
?>
</body>
</html>
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2013-05-02 13:49:56 2013-05-02 14:20:55 2013-05-02 14:22:56
|
|
|
|
|
Date :
2013-05-02 13:49:27 |
By :
lookpla26 |
View :
1116 |
Reply :
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2013-05-02 14:23:32 |
By :
lookpla26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<body>
<?
for($i=1;$i<=(int)$_POST["hdnMaxLine"];$i++)
{
echo $_POST["type_id"][$i];
echo "<br>";
echo $_POST["type_name"][$i];
echo "<br>";
echo $_POST["color_name"][$i];
echo "<br>";
}
?>
</body>
</html>
|
ประวัติการแก้ไข 2013-05-02 14:46:21
|
|
|
|
Date :
2013-05-02 14:44:55 |
By :
Naizan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เปลี่ยนจาก for เป็น foreach ดูนะครับ
บางทีค่า $_POST["hdnMaxLine"] อาจจะส่งมาไม่ครบ เลยวนลูปไม่ได้
Code (PHP)
<?
foreach($_POST["type_id"] as $key=>$typeId)
{
echo "type_id :: " ,$_POST["type_id"][$key];
echo "<br>";
echo "type_name :: " ,$_POST["type_name"][$key];
echo "<br>";
echo "color_name :: " ,$_POST["color_name"][$key];
echo "<br><hr>";
}
?>
|
|
|
|
|
Date :
2013-05-02 15:01:20 |
By :
Naizan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้มาแบบ งงๆ ยังไม่ค่อยเข้าใจคำสั่ง foreach เดี่ยวลองศึกษาดูค่ะ
|
|
|
|
|
Date :
2013-05-02 15:04:24 |
By :
lookpla26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันที่จริง ตอบไว้ในกระทู้เก่า ๆ และบทความที่คุณเอามาแล้วครับ
|
|
|
|
|
Date :
2013-05-02 15:33:32 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
บันทึกได้เเล้วแต่ว่าบันทึกแค่แถวแรก แถวเดียวอ้ะค้ะ
|
|
|
|
|
Date :
2013-05-02 15:50:15 |
By :
lookpla26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|