1

我想post2.php使用 POST 或 GET 方法将变量行与其他 HTML 表单变量一起发送。

下面的代码给出了一个错误:

Notice: Undefined index: row1 in C:\xampp\htdocs\PhpProject1\OtherUsableItems\post2.php on line 8

post1.php

 <html>
 <head>
 <SCRIPT language="javascript">
 function addRow(tableID) {
      var table = document.getElementById(tableID);

      var rowCount = table.rows.length;
      var row = table.insertRow(rowCount);

      var colCount = table.rows[0].cells.length;

      for(var i=0; i<colCount; i++) {
          var newcell = row.insertCell(i);

          newcell.innerHTML = table.rows[0].cells[i].innerHTML;
          //alert(newcell.childNodes);
          switch(newcell.childNodes[0].type) {
              case "text":
                      newcell.childNodes[0].value = "";
                      break;
              case "checkbox":
                      newcell.childNodes[0].checked = false;
                      break;
              case "select-one":
                      newcell.childNodes[0].selectedIndex = 0;
                      break;
          }
      }      
  }
  </script>
  <script>
  function count(tableId){
      var rows = document.getElementById(tableId).getElementsByTagName("TR").length;

     // window.location.href = "http://localhost/PhpProject1/OtherUsableItem  /post2.php?rows=" + rows ;
     // alert('Your table has ' + rows + ' rows.');
     $.post("post2.php", { 'row' : rows}, function(rows){alert('rows'+rows);});
  }
  </script>

  </head>
  <body>
  <form action="post2.php" method="post">

   <TABLE id="dataTable"  border="1">
    <TR>
        <TD> 1 </TD>
        <TD> <INPUT name="n1[]"type="text" /> </TD>
        <TD> <INPUT name="n2[]"type="text" /> </TD>
        <TD><SELECT name="country[]" type="select-one">
                <OPTION value="in">India</OPTION>
                <OPTION value="de">Germany</OPTION>
                <OPTION value="fr">France</OPTION>
                <OPTION value="us">United States</OPTION>
                <OPTION value="ch">Switzerland</OPTION>
            </SELECT></TD>
    </TR>
  </TABLE>
 <INPUT type="button" value="Add Row" onclick="addRow('dataTable');"/>
 <button id="bt" onclick="count('dataTable');">Submit</button>
 </form>

 </body>
 </html>

post2.php

<?php
    $n1 = $_POST['n1'];
    $n2 = $_POST['n2'];
    $country = $_POST['country'];
    echo $n1[0];
    echo $n2[0];
    echo $country[0];
    $count = $_POST['row1'];
    echo $count;
?>
4

2 回答 2

1

尝试更改为'row'而不是'row1'

$n1 = $_POST['n1'];
$n2 = $_POST['n2'];
$country = $_POST['country'];
echo $n1[0];
echo $n2[0];
echo $country[0];
$count = $_POST['row'];
echo $count;

以后用print_r来查看 的值$_POST

除了上述说明之外,我<script>将从 post1.php 中删除第二个标签,并将以下代码放入表单开头的正文中:

<form action="post2.php" method="post" >
  <input id="rowNumber" type="hidden" name="row" value="1"/>

此外,将以下行添加到function addRow

    var rowNumber = document.getElementById('rowNumber');
    rowNumber.value = parseInt( rowNumber.value ) + 1;
于 2013-04-22T19:17:15.127 回答
0

问题是您没有发送正确的 Post 值。检查此行是否:

var rows = document.getElementById(tableId).getElementsByTagName("TR").length;

它返回的值类似于:{name:'value',name2:'value2'}

之后,您将能够使用 $_POST['name'] 通过 php 访问...

这条线:

 $.post("post2.php", { 'row' : rows}, function(rows){alert('rows'+rows);});

替换为:

 $.post("post2.php", rows, function(rows){alert('rows'+rows);});

否则您将使用$_POST['row']

于 2013-04-22T20:03:01.820 回答