Rataiczak24
Rataiczak24

Reputation: 1042

Displaying Data From Dynamic HTML Table

I have an HTML form that you can input values into. You can click submit then it will show the data on a confirmation page that you entered. I was having problems with displaying multiple lines of text but got that fixed. Now I have another problem...I have 3 sets of tables and whenever information is entered (specifically in the 3rd table), it copies that data into the other table...how can this be fixed??

This is the HTML for the table and looks the same for each of the 3 tables:

<table id="tables" cellspacing="5">
    <tr align="center" id="table_titles">
        <td>Tier</td>
        <td>Purchase Minimum</td>
        <td>Multiplier</td>
        <td>UOM</td>
        <td>Retro</td>
        <td>Guaranteed</td>
        <td>Paid</td>
        <td>Delete?</td>
        <td>Add Row</td>
    </tr>
    <tr>
            <td align="center" name="tier">1</td>
            <td><input type="text" id="rebate_tables" data-name="purchase_minimum" name="rows[0][purchase_minimum]"></td>
            <td><input type="text" id="rebate_tables" data-name="multiplier" name="rows[0][multiplier]"></td>
            <td><input type="text" id="rebate_tables" data-name="uom" name="rows[0][uom]"></td>
            <td><input type="text" id="rebate_tables" data-name="retro"  name="rows[0][retro]"></td>
            <td><input type="text" id="rebate_tables" data-name="guaranteed" name="rows[0][guaranteed]"></td>
            <td><input type="text" id="rebate_tables" data-name="paid" name="rows[0][paid]"></td>
            <td><input type="button" id="delRow" value="Delete" onclick="deleteRow(this)"></td>
            <td><input type="button" id="addmoreRowsbutton" value="Add row" onclick="insRow()"></td>
        </tr>
</table>

This is the JAVASCRIPT for each table and is extremely similar to that of the other tables except for the insRow() has a different name as well as the new_row:

function insRow()
{
  var x=document.getElementById('tables');
  var new_row = x.rows[1].cloneNode(true);
  var len = x.rows.length;
  new_row.cells[0].innerHTML = len;

  var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
  inp1.id += len;
  inp1.value = '';
  var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
  inp2.id += len;
  inp2.value = '';

  var inputs = new_row.querySelectorAll('input[type=text]');

  for(var i=0;i<inputs.length;i++)
  {
      inputs[i].value='';
      inputs[i].name='rows[' + len + '][' + inputs[i].dataset.name + ']';
  }

  x.appendChild(new_row)
}

And this is the HTML/PHP confirmation page with each table having the same code:

<?php if(isset($_POST['rows'])): ?>
    <table id="table" cellspacing="20">
        <tr align="center" id="table_row">
            <td>Tier</td>
            <td>Purchase Minimum</td>
            <td>Multiplier</td>
            <td>UOM</td>
            <td>Retro</td>
            <td>Guaranteed</td>
            <td>Paid</td>
        </tr>
        <?php 
            $count = 1;
            foreach($_POST['rows'] as $row): 
        ?>
            <tr align="center">
                <td><?php echo $count; ?></td>
                <td><?php echo $row['purchase_minimum']; ?></td>
                <td><?php echo $row['multiplier']; ?></td>
                <td><?php echo $row['uom']; ?></td>
                <td><?php echo $row['retro']; ?></td>
                <td><?php echo $row['guaranteed']; ?></td>
                <td><?php echo $row['paid']; ?></td>
            </tr>
        <?php 
            $count++;
            endforeach; 
        ?>
    </table>
<?php endif; ?>

Upvotes: 1

Views: 2594

Answers (1)

Al Foиce    ѫ
Al Foиce ѫ

Reputation: 4315

There is a big problem in your html code: several elements have the same ids. Ids must be unique. Each table must have a different id than the other tables. Each input must have a different id than the others.

Upvotes: 1

Related Questions