Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing DOM table variables to PHP
    primarykey
    data
    text
    <p>I am not very experienced just a learner. Now come to the question. I have a dynamic table codes are below: works fine as intended.</p> <pre><code> &lt;head&gt; &lt;style type="text/css"&gt; &lt;!-- #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; } --&gt; &lt;/style&gt; &lt;script type="text/javascript"&gt; 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&lt;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 &lt;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&lt;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&lt;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(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="tableaddrow_nw.php" method="get"&gt; &lt;p&gt; &lt;input type="button" value="Add" onclick="addRowToTable();" /&gt; &lt;input type="button" value="Insert [I]" onclick="insertRowToTable();" /&gt; &lt;!--&lt;input type="button" value="Delete [D]" onclick="deleteChecked();" /&gt;--&gt; &lt;input type="button" value="Submit" onclick="openInNewWindow(this.form);" /&gt; &lt;/p&gt; &lt;table border="0" cellspacing="0" id="tblitemsgrid" width=600&gt; &lt;thead&gt; &lt;tr&gt; &lt;th colspan="5"&gt;Sample table&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;E.#&lt;/th&gt; &lt;th&gt;Account name&lt;/th&gt; &lt;th&gt;Debit&lt;/th&gt; &lt;th&gt;Credit&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt;&lt;/tbody&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This is a processing page:</p> <pre><code> &lt;head&gt; &lt;title&gt;Table Add Row - new window&lt;/title&gt; &lt;script language="JavaScript"&gt; &lt;!-- function printToPage() { var pos; var searchStr = window.location.search; var searchArray = searchStr.substring(1,searchStr.length).split('&amp;'); var htmlOutput = ''; for (var i=0; i&lt;searchArray.length; i++) { htmlOutput += searchArray[i] + '&lt;br /&gt;'; } return(htmlOutput); } //--&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;b&gt;MREDKJ's Table Add Row&lt;/b&gt; &lt;br /&gt; Below should be the name/value pairs that were submitted: &lt;p&gt; &lt;script language="JavaScript"&gt; &lt;!-- document.write(printToPage()); //--&gt; &lt;/script&gt; &lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>the above displays a result:</p> <pre><code>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 </code></pre> <p>how can I pass javascript variables into PHP (which is server side and client side) in the above case ? thanks alot in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload