为什么一些 $_POST 变量传递到刷新的页面,而另一些则没有


Why are some $_POST variables passing to refreshed page and others are not?

我通过在单独的循环中将所有值添加到数组来解决此问题。 感谢您的帮助。

$_POST['val1']变量将传递到刷新的页面,但$_POST['val2']不会。 每个代码都是相同的,并且它们都在同一个表单中。 欢迎任何想法。 在这一点上,我想我真的只需要一双新的眼睛来看待这个问题。

我只是包含代码片段,以便更容易查看。 程序中的其他所有内容都正常运行,并且我没有收到任何特定错误......只是$_POST['val2']的值没有打印。

下面是定义$_POST变量的代码:

// foreach ($line as $col_value) ...
if ($counter == 1):
    echo "'t't<input type='hidden' name='val1'   value='$col_value' />";
elseif ($counter == 2):
    echo "'t't<input type='hidden' name='val2' value='$col_value' />";
endif;

以下是使用它们的代码:

for($i=1; $i<6; $i++) {
    echo "'t<tr>'n";
    echo "'t't<td>";
    if($i == 1){
        echo "id";
        echo "</td>'n";
        echo "'t't<td>" . $_POST['val1'] . "</td>'n";
    } elseif($i == 2){
        echo "name";
        echo "</td>'n";
        echo "'t't<td>" . $_POST['val2'] . "</td>'n";

我可能建议分成 2 种形式,看看你想完成什么。
您希望在输出中显示什么?
这段代码似乎是一个自我发布。
此外,将隐藏的文本框更改为普通的文本,以便您可以看到代码发生了什么。

  <form action=""  method="post">
  <?php
  $counter = 1; //default values are always good to have
  $col_value = 0;
  $col_value = @$_POST['val1'] ;
  if ($counter == 1)
      echo "'t't<input  name='val1'   value='$col_value' />";
  else 
      echo "'t't<input  name='val2' value='$col_value' />";
  //I would rather write it like this:
  $line = 1;
  $counter = 1; //give it a default value to start off with.
  $col_value = @$_POST['val1'] ;
  $col_value = 0;
  $line = array(1, 2,4);
     foreach ($line as $col_value) {
     echo "Current value of '$line: $col_value.'n";
  echo "<input  name='val1' value='$col_value' />";
  echo "<input  name='val2' value='$col_value' />";
     }
  ?>
  <input type="submit" name="submit" value="Submit" />
  <br>
  <br>
  <table>
  <?php
  $val1 = $_POST['val1'] ;
  $val2 = $_POST['val2'] ;
  echo "POSTval1: ".$val1."<br>";
  echo "POSTval2: ".$val2."<br>";
  for($i=1; $i<6; $i++) {
      echo "'t<tr>'n";
      echo "'t't<td>";
      if($i == 1){
          echo "id";
          echo "</td>'n";
          echo "'t't<td>" . $val1 . "</td>'n";
      } elseif($i == 2){
          echo "name";
          echo "</td>'n";
          echo "'t't<td>" . $val2 . "</td>'n";
      }
  } //don't forget the closing brackets.
  ?>
  </table>
  </form>

为了在这里有一个详细的答案,我将回答我自己的问题。

这是我如何解决这个问题的一个例子。 此循环在表单之前完成:

    if($table == 'language'){                       //this cycles through query to get value of $language... but it only gets last value of language from search query
        $numRow = 0;
        //results are good so output them to HTML
        while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
            $counter = 0;
            foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
                if($counter == 1){
                    $language[$numRow] = $col_value;//array($numRow => $col_value);
                    $counter++;
                }
                else
                    $counter++;
            }
            $numRow++;
        }

这就是在表单中添加信息的方式:

        echo "'t't<input type='"hidden'" name='"language'" value='"$language[$numRow]'" />";