2012-02-14 28 views
0

我試圖在表格#itemsTable中存儲內容tr,在數組celValues中並將其發佈到php並重復它(在下一個tr之前直到最後一個tr)。 這裏是代碼:分開發布html表的行到php

$('#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++; 
     } 
?> 

問題是重複的,而不是發佈所有TR的內容第一行。

輸出3行具有不同contents.but第一行重複3次:

1000網站設計1 100.5 100.5 1000Website Design1100.5100.5 1000網站設計1 100.5 100.5

+0

仔細檢查foreach循環,你打印'$ arr'而不是'$ val'。另外,一個好的調試技術是使用'print_r($ array)'來查看數組的原始內容。 – Moshe 2012-02-14 05:15:39

回答

1

似乎沒有在你的代碼點通過TRS迭代,因爲你沒有利用它們

也許你的意思

$('#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(); 

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

在test.php的

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

響應您的評論這裏是一個DEMO序列化形式將有值,所有輸入字段

+0

我想單獨發佈每個tr以便將其保存在數據庫中。 – dynid 2012-02-14 05:20:00