Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add row to table script, repeat value?

Status
Not open for further replies.

piznac

IS-IT--Management
Joined
May 5, 2006
Messages
3
Location
US
Hi, new guy here. Ok I have this Add Row to Table script:

Code:
<script language="JavaScript" type="text/javascript">
// Last updated 2006-02-21
function addRowToTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  
  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode(iteration);
  cellLeft.appendChild(textNode);
  
    //2nd cell to left
  var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'rm[]';
  el.id = 'rm' + iteration;
  el.size = 20; 
    
  cellRight.appendChild(el);
  
  // right cell
  var cellRightb = row.insertCell(2);
  var el2 = document.createElement('input');
  el2.type = 'text';
  el2.name = 'dm[]'; 
  el2.id = 'dm' + iteration; 
  el2.size = 20; 

  cellRightb.appendChild(el2);
  
  //3rd cell to left
  var cellRightc = row.insertCell(3);
  var el3 = document.createElement('input');
  el3.type = 'text';
  el3.name = 'store[]';
  el3.id = 'store' + iteration;
  el3.size = 8; 
  

  cellRightc.appendChild(el3);
  
  //4th cell to left
  var cellRightd = row.insertCell(4);
  var el4 = document.createElement('input');
  el4.type = 'text';
  el4.name = 'pto[]';
  el4.id = 'pto' + iteration;
  el4.size = 10; 
  

  cellRightd.appendChild(el4);
  
  //5th cell to left
  var cellRighte = row.insertCell(5);
  var el5 = document.createElement('input');
  el5.type = 'text';
  el5.name = 'pda[]';
  el5.id = 'pda' + iteration;
  el5.size = 10; 
  

  cellRighte.appendChild(el5);
  
  //6th cell to left
  var cellRightf = row.insertCell(6);
  var el6 = document.createElement('input');
  el6.type = 'text';
  el6.name = 'date[]';
  el6.id = 'date' + iteration;
  el6.size = 20; 
  

  cellRightf.appendChild(el6);
}
</script>

And I have the coresponding table in my HTML. But the first three text fields data is generated by php and a mysql DB.

My question is how would I repeat that dynamic value? Example:

[URL unfurl="true"]http://www.connerandassociates.com/pto-pda/rm.php[/URL]

The first 3 pages are just to setup the data for the last page. Please be careful this is very much live. But as you can see the first 3 fields have this:

Code:
<?php echo $row_Recordset1['rm']; ?>

Or somthing very similar. And the date field has a calendar JS attached as well. Is there a way to repeat these along with the add new row? I have tried this:

Code:
var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'rm[]';
  el.id = 'rm' + iteration;
  el.size = 20; 
  el.value = '<?php echo $row_ptopdalisting['rm']; ?>'

But when it repeated the row, none of the text celss showed up.

Any help would be great-- thanks!!
 
I am not certain but I think your problem is with the use of the name rm[]. Javascript does not like the brackets as part of a name, they are reserved characters.

Try altering your code to not use the brackets within your javascript setting the name of the fields to see if your cells show up. That's not a solution but it will tell you if the brackets are the source of this particular problem.

Those brackets are a common problem between PHP and Javascript and it has come up in this forum twice in the last week or so that I have seen. I believe there is a PHP side workaround to using those brackets in the names but do not know what it is. If you find out this IS indeed your problem and you find a fix please post it here as it will come in handy to know.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Im using the [] for an array to insert the data into a DB. Thanks for the help though.
 
Yes, I understand that you use it for an array but the brackets cause Javascript to not function. My suggestion to remove from the field names when you generate the cells is just a test to see if the javascript will work properly without them there and thereby prove where the problem is rather than just guessing. It is not the solution it is only a test that would only take a minute to run and make it much clearer what the cause of the problem is.

I know how it is when you have something working and when you try to add something else in that doesnt work people tell you that you have to re-write the working piece of your code to fix the problem. The first instinct is to say "Nope, dont want to do that I will keep looking for a way around the problem from this side." But you DO need to identify exactly what the problem is before you can fix it and that is all I suggested.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Ok, sry I didnt understand.

I tried that and no luck. It still did the same thing. But my questions is should I be able to add the value to the portion of the script?

Code:
var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'rm[]';
  el.id = 'rm' + iteration;
  el.size = 20; 
  el.value = '<?php echo $row_ptopdalisting['rm']; ?>'
 
I would have thought you could yes.
The PHP code will evaluate on the page before the javascript ever runs and the text inside el.value = '<?php echo $row_ptopdalisting['rm']; ?>' should get replaced with the value.

Is the value $row_ptopdialisting['rm'] an array or just a single value?

Try executing this after the entire page has finished loading (and you execute the code above to enter the value).

alert(document.getElementById('rm[]').innerHTML);

That should show you any content that was written to the cell.



It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top