2011-07-16 64 views
0

我不是很有经验的学习者。 现在来回答这个问题。 我有一个动态表代码如下:工作正常。将DOM表变量传递给PHP

<head> 

    <style type="text/css"> 
    <!-- 
    #tblitemsgrid 
    td { 
    padding: 0.5em; 
    } 
    .classy0 { background-color: #234567; color: #89abcd; } 
    .classy1 { background-color: #89abcd; color: #234567; } 

    th { 
    padding: 0.5em; 
    background:#39C; 
    text-align:center; 




    } 

    --> 
    </style> 
    <script type="text/javascript"> 

    var INPUT_NAME_PREFIX = 'input'; // this is being set via script 
    var RADIO_NAME = 'totallyrad'; // this is being set via script 
    var TABLE_NAME = 'tblitemsgrid'; // this should be named in the HTML 
    var ROW_BASE = 1; // first number (for display) 
    var hasLoaded = false; 

    window.onload=fillInRows; 

    function fillInRows() 
    { 
    hasLoaded = true; 
    addRowToTable(); 

    } 

    // CONFIG: 
    // myRowObject is an object for storing information about the table rows 
    function myRowObject(one, two, three, four, five) 
    { 
    this.one = one; // text object 
    this.two = two; // text object 
    this.three = three; // text object 
    this.four = four; // text object 


    } 

    /* 
    * insertRowToTable 
    * Insert and reorder 
    */ 
    function insertRowToTable() 
    { 
    if (hasLoaded) { 
    var tbl = document.getElementById(TABLE_NAME); 
    var rowToInsertAt = tbl.tBodies[0].rows.length; 
    for (var i=0; i<tbl.tBodies[0].rows.length; i++) { 
    } 
    addRowToTable(rowToInsertAt); 
    reorderRows(tbl, rowToInsertAt); 

    } 
    } 

    /* 
    * addRowToTable 

    function addRowToTable(num) 
    { 
    if (hasLoaded) { 
    var tbl = document.getElementById(TABLE_NAME); 
    var nextRow = tbl.tBodies[0].rows.length; 
    var iteration = nextRow + ROW_BASE; 
    if (num == null) { 
    num = nextRow; 
    } else { 
    iteration = num + ROW_BASE; 
    } 

    // add the row 
    var row = tbl.tBodies[0].insertRow(num); 

    // CONFIG: requires classes named classy0 and classy1 
    row.className = 'classy' + (iteration % 2); 

    // CONFIG: This whole section can be configured 

    // cell 0 - text - Serial Number 
    var cell0 = row.insertCell(0); 
    var textNode = document.createTextNode(iteration); 
    cell0.appendChild(textNode); 

    // cell 1 - input text - Account name 
    var cell1 = row.insertCell(1); 
    var txtInpACC = document.createElement('input'); 
    txtInpACC.setAttribute('type', 'text'); 
    txtInpACC.setAttribute('name', 'accname' + iteration); 
    txtInpACC.setAttribute('size', '40'); 
    txtInpACC.setAttribute('value', iteration); 
    cell1.appendChild(txtInpACC); 

    // cell 2 - input box- Dr amount 
    var cell2 = row.insertCell(2); 
    var txtInpDR = document.createElement('input'); 
    txtInpDR.setAttribute('type', 'text'); 
    txtInpDR.setAttribute('name', 'DrAmount' + iteration); 
    txtInpDR.setAttribute('size', '10'); 
    txtInpDR.setAttribute('value', iteration); // iteration included for debug purposes 
    cell2.appendChild(txtInpDR); 


    // cell 3 - input box- Cr amount 
    var cell3 = row.insertCell(3); 
    var txtInpCR = document.createElement('input'); 
    txtInpCR.setAttribute('type', 'text'); 
    txtInpCR.setAttribute('name', 'CrAmount' + iteration); 
    txtInpCR.setAttribute('size', '10'); 
    txtInpCR.setAttribute('value', iteration); // iteration included for debug purposes 
    cell3.appendChild(txtInpCR); 


    // cell 4 - input button - Delete 
    var cell4 = row.insertCell(4); 
    var btnEl = document.createElement('input'); 
    btnEl.setAttribute('type', 'button'); 
    btnEl.setAttribute('value', 'Delete'); 
    btnEl.onclick = function() {deleteCurrentRow(this)}; 
    cell4.appendChild(btnEl); 


    // Pass in the elements you want to reference later 
    // Store the myRow object in each row 
    row.myRow = new myRowObject(textNode, txtInpACC, txtInpDR, txtInpCR, btnEl); 
    } 
    } 

    // CONFIG: this entire function is affected by myRowObject settings 

    function deleteCurrentRow(obj) 
    { 
    if (hasLoaded) { 
    var oRows = document.getElementById('tblitemsgrid').getElementsByTagName('tr'); 
    var iRowCount = (oRows.length - 2); 

    if (iRowCount <1+1) { 
    alert('Your table has ' + iRowCount + ' row(s). Row count can not be zero.'); 
    return 
    } 

    var delRow = obj.parentNode.parentNode; 
    var tbl = delRow.parentNode.parentNode; 
    var rIndex = delRow.sectionRowIndex; 
    var rowArray = new Array(delRow); 
    deleteRows(rowArray); 
    reorderRows(tbl, rIndex);} 

    } 

    function reorderRows(tbl, startingIndex) 
    { 
    if (hasLoaded) { 
    if (tbl.tBodies[0].rows[startingIndex]) { 
    var count = startingIndex + ROW_BASE; 
    for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) { 

    // CONFIG: next line is affected by myRowObject settings 
    tbl.tBodies[0].rows[i].myRow.one.data = count; // text 

    // CONFIG: next line is affected by myRowObject settings 
    tbl.tBodies[0].rows[i].myRow.two.name = INPUT_NAME_PREFIX + count; // input text 

    var tempVal = tbl.tBodies[0].rows[i].myRow.two.value.split(' '); 
    tbl.tBodies[0].rows[i].myRow.two.value = count + ' was' + tempVal[0]; 

    // CONFIG: next line is affected by myRowObject settings 
    tbl.tBodies[0].rows[i].myRow.four.value = count; // input radio 

    // CONFIG: requires class named classy0 and classy1 
    tbl.tBodies[0].rows[i].className = 'classy' + (count % 2); 

    count++; 
    } 
    } 
    } 
    } 

    function deleteRows(rowObjArray) 
    { 
    if (hasLoaded) { 
    for (var i=0; i<rowObjArray.length; i++) { 
    var rIndex = rowObjArray[i].sectionRowIndex; 
    rowObjArray[i].parentNode.deleteRow(rIndex); 
    } 
    } 
    } 

    function openInNewWindow(frm) 
    { 
    // open a blank window 
    var aWindow = window.open('', 'TableAddRow2NewWindow', 
    'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400'); 

    // set the target to the blank window 
    frm.target = 'TableAddRow2NewWindow'; 

    // submit 
    frm.submit(); 
    } 
    </script> 
    </head> 
    <body> 
    <form action="tableaddrow_nw.php" method="get"> 
    <p> 
    <input type="button" value="Add" onclick="addRowToTable();" /> 
    <input type="button" value="Insert [I]" onclick="insertRowToTable();" /> 
    <!--<input type="button" value="Delete [D]" onclick="deleteChecked();" />--> 
    <input type="button" value="Submit" onclick="openInNewWindow(this.form);" /> 
    </p> 
    <table border="0" cellspacing="0" id="tblitemsgrid" width=600> 
    <thead> 
    <tr> 
    <th colspan="5">Sample table</th> 
    </tr> 
    <tr> 
    <th>E.#</th> 
    <th>Account name</th> 
    <th>Debit</th> 
    <th>Credit</th> 
    <th></th> 
    </tr> 
    </thead> 
    <tbody></tbody> 
    </table> 
    </form> 
    </body> 
    </html> 

这是一个处理页:

<head> 
    <title>Table Add Row - new window</title> 
    <script language="JavaScript"> 
    <!-- 
    function printToPage() 
    { 
    var pos; 
    var searchStr = window.location.search; 
    var searchArray = searchStr.substring(1,searchStr.length).split('&'); 
    var htmlOutput = ''; 
    for (var i=0; i<searchArray.length; i++) { 
    htmlOutput += searchArray[i] + '<br />'; 
    } 
    return(htmlOutput); 
    } 
    //--> 
    </script> 
    </head> 

    <body> 
    <b>MREDKJ's Table Add Row</b> 
    <br /> 
    Below should be the name/value pairs that were submitted: 
    <p> 
    <script language="JavaScript"> 
    <!-- 
    document.write(printToPage()); 
    //--> 
    </script> 
    </p> 
    </body> 
    </html> 

上述显示器的结果:

accname1=1 
DrAmount1=1 
CrAmount1=1 
input2=2+was2 
DrAmount2=2 
CrAmount2=2 
input3=3+was3 
DrAmount3=3 
CrAmount3=3 
input4=4+was4 
DrAmount4=4 
CrAmount4=4 

如何传递的JavaScript变量到PHP(这是服务器侧和客户端侧)在上述情况下? 提前感谢。

回答

0

您可以使用jQueryajax将这些信息传递给服务器简单的方法

和记忆。 PHP不是客户端语言

$.ajax({ 
     'url': 'ajax.php', 
     'type': 'POST', 
     'data': 'accname1='+accname1+'&input1='+input1+'&input2='+input2+'&input3='+input3+'&DrAmount1='+DrAmount1+'&DrAmount2='+DrAmount2+'&DrAmount3='+DrAmount3+'&CrAmount1='+CrAmount1'&CrAmount2='+CrAmount2'&CrAmount3='+CrAmount3, 
     'success': function(){ 
      alert('data sent'); 
     } 
}); 
+0

感谢您的回答。上面的示例动态值中是否有可能具有“数据”?因为用户可能有五组行来输入,例如accname1,accname2,accname3等等。我希望我能够清楚地解释我自己。道歉,因为我是新手。 – user837168

+0

我的答案左侧有接受按钮 – genesis

1

将变量从客户端传递到服务器端的方式是通过HTTP请求。因此,要么重定向到传递变量的PHP页面作为GET查询字符串或POST数据,要么您也可以执行GET或POST的AJAX调用。

+0

感谢您的回复。你能指点我一个例子吗?干杯 – user837168

+0

@pixelfreak也许你可以解释一下这个问题:http://stackoverflow.com/q/14165920/1788087 – Ben