Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将html表的行分别发布到php_Php_Jquery - Fatal编程技术网

将html表的行分别发布到php

将html表的行分别发布到php,php,jquery,Php,Jquery,我试图将tr中的内容存储在表#itemsTable中的数组celValues中,然后将其发布到php并重复(将下一个tr发布到最后一个tr)。 以下是代码: $('#save').click(function(){ //alert("hi"); var celValues= new Array(); $('#itemsTable tr').each(function() { celValues[0] = $('td:nth-child(2)').fi

我试图将tr中的内容存储在表#itemsTable中的数组celValues中,然后将其发布到php并重复(将下一个tr发布到最后一个tr)。 以下是代码:

$('#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行,但第一行重复3次:

1000网站设计1100.5100.5 1000网站设计1100.5100.5 1000网站设计1100.5 100.5 100.5

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

作为对您评论的回应,这里是一个示例,序列化表单将具有所有输入字段的值

在test.php中

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


作为对您评论的回应,这里是一个示例,序列化表单将具有所有输入字段的值

,您的代码中似乎没有迭代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();

....

您的代码中似乎没有迭代trs的意义,因为您没有使用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();

....

再次检查foreach循环,您打印的是
$arr
,而不是
$val
。另外,一种很好的调试技术是使用
print\r($array)
查看数组的原始内容。再次检查每个循环是否打印出
$arr
,而不是
$val
。另外,一种很好的调试技术是使用
print\r($array)
查看数组的原始内容。我想分别发布每个tr以将其保存在数据库中。我想分别发布每个tr以将其保存在数据库中。