0

Is it somehow possible to get table every row first cell input value and send it over ajax to PHP and it would send this information to mysql?

<table id="kogused">
<tbody>
  <tr>
    <td>12</td>
    <td>empty</td>
  </tr>
  <tr>
    <td>34</td>
    <td>empty</td>
  </tr>
</tbody>
</table>

$('.addtodb').click(function(){
  $('#kogused tbody tr td.addr input').each(function() {
      var text = $(this).val();
  $.ajax({
        type: "POST",
        url: "insert/insert_addr.php",
        data: "aadress="+ text,
        success: function(){
            alert('Õnnestus!');
            }
        });
        return false;
    });             
});

In this way - it adds two rows to DB but with the same value

4
  • Why not batch the POST, instead of firing a separate one per cell?
    – Pierreten
    Jan 4, 2011 at 9:00
  • I'd probably flatten the table's data into JSON, and send it to the server in one go (unless the tables huge, then i'd batch the POSTs into reasonable chunks.) It's pretty inefficient to send that small amount of data that many times to the server.
    – Pierreten
    Jan 4, 2011 at 9:07
  • you're probably right because the table is usually at most 2-8 rows and every row not over 6 chars. I'm gonna try something with JSON. thanks Jan 4, 2011 at 9:11
  • any time, i'd write it out for you, but i'd be answering a separate issue.
    – Pierreten
    Jan 4, 2011 at 9:13

1 Answer 1

1

Is it somehow possible to get table every row first cell input value and send it over ajax to php and it would send this information to mysql?

To get every first cell, you would use the :first or :nth-child like this:

$('#kogused tbody tr td:first input')

Or

$('#kogused tbody tr td:nth-child(1n) input')
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.