|
|
|
ถามพี่ๆเรื่อง การใช้ ajax auto fill ร่วมกับ ปุ่มเพิ่ม text box ค่ะ |
|
|
|
|
|
|
|
newCell.setAttribute("OnChange", "doCallAjax('txtProductID3','txtProductName3','txtPrice3');");
ประมาณนี้ครับ
|
|
|
|
|
Date :
2012-04-18 06:27:51 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
หรือไม่ก็แทรกใน HTML Tag เลยครับ
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"Column5_"+intLine+"\" VALUE=\"\" OnChange=\"JavaScript:doCallAjax('txtProductID3','txtProductName3','txtPrice3');\"></center>"
ประมาณนี้ครับ ดู Syntax ให้ถูกด้วย
|
|
|
|
|
Date :
2012-04-18 06:28:57 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จากโค้ดนี้
Code (PHP)
<? $link = mysql_connect("localhost", "root", "password");
mysql_query("use project;");
mysql_query("set NAMES tis620");
$strsql = "select * from nutrients";
$objquery = mysql_query($strsql); ?>
<script language="javascript">
function CreateSelectOption(ele)
{
var objSelect = document.getElementById(ele);
var Item = new Option("", "");
objSelect.options[objSelect.length] = Item;
<?
while($objResult = mysql_fetch_array($objquery))
{
?>
var Item = new Option("<?=$objResult["NutrientName"];?>", "<?=$objResult["NutrientID"];?>");
objSelect.options[objSelect.length] = Item;
<?
}
?>
}
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 1 ***//
newCell = newRow.insertCell(0);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><SELECT NAME=\"txtnutr_"+intLine+"\" ID=\"txtnutr_"+intLine+"\ value=\"\" OnChange=\"JavaScript:doCallAjax('txtnutr_"+intLine+"','txtuni_"+intLine+"');\"></SELECT></center>";
//*** Create Option ***//
CreateSelectOption("txtnutr_"+intLine)
document.frmMain.hdnMaxLine.value = intLine;
//*** Column 2 ***//
newCell = newRow.insertCell(1);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"txtvolu_"+intLine+"\" ID=\"txtvolu_"+intLine+"\" VALUE=\"\"></center>";
//*** Column 3 ***//
newCell = newRow.insertCell(2);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"txtuni_"+intLine+"\" ID=\"txtuni_"+intLine+"\" VALUE=\"\"></center>";
}
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;
}
}
var HttPRequest = false;
function doCallAjax(fNutrientID,fUnit) {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'AjaxGetFill.php';
var pmeters = "tNutrientID=" + encodeURI( document.getElementById(fNutrientID).value);
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
//if(HttPRequest.readyState == 3) // Loading Request
//{
//document.getElementById(fProductName).innerHTML = "..";
//}
if(HttPRequest.readyState == 4) // Return Request
{
var myProduct = HttPRequest.responseText;
if(myProduct != "")
{
var myArr = myProduct.split("|");
document.getElementById(fUnit).value = myArr[0];
}
}
}
}
</script>
ตอนแรกก็ยัง query ข้อมูลมาแสดงใน dropdownlistได้ แต่พอหนูเพิ่มบรรทัด นี้เข้าไป ก็กลายเป็นอย่างภาพข้างล่างเลยค่ะ ไม่ทราบว่าผิดตรงไหนคะ
Code (PHP)
newCell.innerHTML = "<center><SELECT NAME=\"txtnutr_"+intLine+"\" ID=\"txtnutr_"+intLine+"\ value=\"\" OnChange=\"JavaScript:doCallAjax('txtnutr_"+intLine+"','txtuni_"+intLine+"');\"></SELECT></center>";
|
|
|
|
|
Date :
2012-04-18 13:32:22 |
By :
Supergirl |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แก้โค้ดให้ใหม่ครับ ลองดูน่ะครับ
Code (PHP)
newCell.innerHTML = "<center><SELECT NAME=\"txtnutr_"+intLine+"\" ID=\"txtnutr_"+intLine+"\" value=\"\" OnChange=\"JavaScript:doCallAjax('txtnutr_"+intLine+"','txtuni_"+intLine+"');\"></SELECT></center>";
|
|
|
|
|
Date :
2012-04-19 16:35:27 |
By :
sakuraei |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|