dynid
dynid

Reputation: 117

posting rows of html table separately to php

I'm trying to store contents a tr in table #itemsTable, in array celValues and post it to php and repeat it (post next tr until last tr). here is code:

$('#save').click(function(){
    //alert("hi");
    var celValues= new Array();
    $('#itemsTable tr').each(function() {
           celValues[0] = $('td:nth-child(2)').find('input').val();
           celValues[1] = $('td:nth-child(3)').find('input').val();
           celValues[2] = $('td:nth-child(4)').find('input').val();
           celValues[3] = $('td:nth-child(5)').find('input').val();
           celValues[4] = $('td:nth-child(6)').find('input').val();

           $.post("test.php", {celvalues: celValues},function(data){

                 document.write(data);

           });

    }); 

});

test.php:

<?php
  $i=0;
  $arr= array();
  $arr = $_POST['celvalues'];
  foreach($arr as $val)
         {
             echo " " .$arr[$i];
             $i++; 
         }
?> 

problem is it repeats first row instead of posting contents of all tr.

output for 3 row with different contents.but first row repeated 3 times:

1000 Website Design 1 100.5 100.5 1000Website Design1100.5100.5 1000 Website Design 1 100.5 100.5

Upvotes: 0

Views: 608

Answers (2)

Rafay
Rafay

Reputation: 31033

$('#save').click(function(){  
           $.post("test.php", {form: $("#yourFromID").serialize()},function(data){
                 document.write(data);
           });   
});

in test.php

<?php
  $i=0;
  $arr= array();
  $arr = $_POST['form'];
  foreach($arr as $val)
         {
             echo " " .$arr[$i];
             $i++; 
         }
?> 

in response to your comment here is a DEMO the serialized form will have the values for all input fields

Upvotes: 2

Vigrond
Vigrond

Reputation: 8198

there doesn't seem to be a point in your code for iterating through the trs because you're not utilizing them

perhaps you meant

 $('#itemsTable tr').each(function(index, element) {
           celValues[0] = $(element).find('td:nth-child(2)').find('input').val();
           celValues[1] = $(element).find('td:nth-child(3)').find('input').val();

....

Upvotes: 1

Related Questions