admin管理员组

文章数量:1295866

I'm trying to grab the cell values from an HTML table so that I can save them into a MySQL table via a call to PHP. I'd like to use JavaScript rather than jQuery.

I'm giving each TR a distinct ID based on the MySQL table's ID field. Also, each TD input cell has a unique ID based on the MySQL table's ID field. Not sure if I need all those, but I used them.

How do I grab the cell's value so that I can pass it to a PHP procedure(I know how to code this PHP) to save the data into MySQL? My JavaScript code below grabs the "innerHTML" but I need to grab the cell value's instead.

For example, if I have 3 rows of data from a MySQL table, with fields id and amount, with values below, how do I grab the "3" and the "1.99"?:

id    amount
-------------
1     100.00
2     9.99
3     1.99

Here is a sample of the PHP/HTML code for the table, submit button and onclick call: (this is a very simplified version of my actual code but should convey the idea)

<?php
    /* define mysql database connect, query and execute it returning result set into $result, id=integer primary key, is_active=tinyint(0 or 1), amount=decimal(12,2) */
    /* ... */    
    /* output form/table */
    echo "<form id='myform' name='myform' >" ;    
        echo "<table id='mytable' name='mytable'>" ;
            while($row = mysql_fetch_array($result))
            {
                $id                 = $row['id'] ;
                $amount             = $row['amount'] ;                
                $tr_id              = "tr_id_" . trim((string)$id) ;                                                
                $td_id              = "td_id_" . trim((string)$id) ;  
                $td_amount_id       = "td_amount_id_" . trim((string)$id) ;                 
                $input_amount_id    = "input_amount_id_" . trim((string)$id) ;
                echo "<tr id='$tr_id' name='$tr_id'>";
                    echo "<td id='$td_id_account' > $id </td>" ; 
                    echo "<td id='$td_amount_id' > <input type='text' id='$input_amount_id' value=$amount > $amount </td>" ;             
                echo "</tr>";
            }
        echo "</table>";
    echo "</form>" ; 
    /* submit button and onclick call */
    echo "<br />" ;
    echo "<table>";
        echo "<tr>" ;
            echo "<td>";
                echo "<button type='button' " . 'onclick="SubmitTableData(\'' . "mytable" .'\');"' . ">Submit</button>" ;
            echo "</td>" ;
        echo "</tr>";
    echo "</table>";         
?>

And here is a sample of my JavaScript function used to loop through the rows of the HTML table:

function SubmitTableData(TableID)
{
    var table = document.getElementById(TableID);
    for (var i = 0, row; row = table.rows[i]; i++) 
    {
       // iterate through rows
       for (var j = 0, col; col = row.cells[j]; j++) 
       {
         // iterate through columns
            alert(col.innerHTML);
       }  
       /* call PHP procedure with row's cell values as parameters */
       /* ... */
       break;
    }
} 

I'm trying to grab the cell values from an HTML table so that I can save them into a MySQL table via a call to PHP. I'd like to use JavaScript rather than jQuery.

I'm giving each TR a distinct ID based on the MySQL table's ID field. Also, each TD input cell has a unique ID based on the MySQL table's ID field. Not sure if I need all those, but I used them.

How do I grab the cell's value so that I can pass it to a PHP procedure(I know how to code this PHP) to save the data into MySQL? My JavaScript code below grabs the "innerHTML" but I need to grab the cell value's instead.

For example, if I have 3 rows of data from a MySQL table, with fields id and amount, with values below, how do I grab the "3" and the "1.99"?:

id    amount
-------------
1     100.00
2     9.99
3     1.99

Here is a sample of the PHP/HTML code for the table, submit button and onclick call: (this is a very simplified version of my actual code but should convey the idea)

<?php
    /* define mysql database connect, query and execute it returning result set into $result, id=integer primary key, is_active=tinyint(0 or 1), amount=decimal(12,2) */
    /* ... */    
    /* output form/table */
    echo "<form id='myform' name='myform' >" ;    
        echo "<table id='mytable' name='mytable'>" ;
            while($row = mysql_fetch_array($result))
            {
                $id                 = $row['id'] ;
                $amount             = $row['amount'] ;                
                $tr_id              = "tr_id_" . trim((string)$id) ;                                                
                $td_id              = "td_id_" . trim((string)$id) ;  
                $td_amount_id       = "td_amount_id_" . trim((string)$id) ;                 
                $input_amount_id    = "input_amount_id_" . trim((string)$id) ;
                echo "<tr id='$tr_id' name='$tr_id'>";
                    echo "<td id='$td_id_account' > $id </td>" ; 
                    echo "<td id='$td_amount_id' > <input type='text' id='$input_amount_id' value=$amount > $amount </td>" ;             
                echo "</tr>";
            }
        echo "</table>";
    echo "</form>" ; 
    /* submit button and onclick call */
    echo "<br />" ;
    echo "<table>";
        echo "<tr>" ;
            echo "<td>";
                echo "<button type='button' " . 'onclick="SubmitTableData(\'' . "mytable" .'\');"' . ">Submit</button>" ;
            echo "</td>" ;
        echo "</tr>";
    echo "</table>";         
?>

And here is a sample of my JavaScript function used to loop through the rows of the HTML table:

function SubmitTableData(TableID)
{
    var table = document.getElementById(TableID);
    for (var i = 0, row; row = table.rows[i]; i++) 
    {
       // iterate through rows
       for (var j = 0, col; col = row.cells[j]; j++) 
       {
         // iterate through columns
            alert(col.innerHTML);
       }  
       /* call PHP procedure with row's cell values as parameters */
       /* ... */
       break;
    }
} 
Share Improve this question edited Jan 11, 2018 at 17:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 25, 2012 at 18:34 ElliotElliot 311 gold badge1 silver badge5 bronze badges 2
  • 1 Why are you doing this? There has to be a simpler way to do what you are trying to acplish. Also, please post the generated HTML for the table, not the php that generates it. – user1726343 Commented Oct 25, 2012 at 18:36
  • My eyes.. that looks terribly plicated for no reason – Nick Fury Commented Oct 25, 2012 at 18:37
Add a ment  | 

4 Answers 4

Reset to default 3

Use the innerText property for the td. Here's a fiddle that shows how to use it.

HTML

<table>
    <tr>
        <td id="1">Bla</td>
        <td id="2"><input id="txt" />Ble</td>
    </tr>
</table>​

JavaScript

var td = document.getElementById('1');
alert(td.innerText);
var td2 = document.getElementById('2');
alert(td2.innerText);​

How to grab cell values from a table?

Update to address Elliots ment

See how to grab the cell value (innerText and data-value) in my new demo

In the table the attribute data-value is used to store some data (2B, 3C,..).

<table id="t2">    
  <thead>
   <tr id="tr1"><th>Student Name<th>Course</tr> 
  </thead>
  <tbody>
    <tr id="tr2"><td data-value="2B">Bert2  <td>Economics  </tr>
    <tr id="tr3"><td data-value="3C">Clyde3 <td>Chemics    </tr>
    <tr id="tr4"><td data-value="4D">       <td>Digital Art</tr>
    <tr id="tr5"><td data-value="5E">Ernest <td>Ecmoplasma </tr>
  </tbody>
</table> 

With jQuery and the right selector you can iterate over each cell:

function eachCell(){
    var cellInnerText = [];
    var cellValue = [];
    var out = document.getElementById("out");
    var out2 = document.getElementById("out2");
    // iterate over each row in table with id t2 in the table-body (tbody)
    $('#t2 tbody tr').each(function(index){     
       // copy the text into an array
       cellInnerText.push($(":first-child", $(this)).text());
       //copy the data-value into an array
       cellValue.push($(":first-child", $(this)).attr('data-value'));
    });
    // show content of both arrays
    out.innerHTML = cellInnerText.join(" | ");
    out2.innerHTML = cellValue.join(" | "); 
}
    

Alert the innerHTML of the first cell in the table's first row

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<p>Click the button to alert the innerHTML of the first cell (td element with index 0) in the table's first row.</p>

<table id="myTable">
  <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
  </tr>
  <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
  </tr>
  <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
  </tr>
</table>
<br> 

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);
}
</script>

</body>
</html> 

Place IDs on Your inputs with some pattern. for example <input type="text" id="input_0_0" name="whatever" /> where 0 is row and second 0 is column and then instead your alert type

v = document.getElementById('input_'+row+'_'+col).value;

It should help You get rolling now

本文标签: phpHow to grab the values from an HTML table39s cells using JavaScriptStack Overflow