从表单发送的另一个页面中检索数组


retrieve an array from another page sent by a form

我想通过更改表单的值(输入)来传递数组。用户可以更改值​​在输入中,如何检索已更改的数组?

例如。。。

  <?php $vector = array("product1" => 150, "product2" => 120); ?>
  <table>
  <form action="page2.php" method="get">
  <?php foreach ($vector as $key => $value) { 
 echo "<tr><td>Product: $name</td><td><input type='text' name='$key' 
     value='$value'/>  
     </td>";
  }
  <tr>
<td><input type="submit" name="process" value="Submit" /></td>
  </tr>
  </form>
  </table>
  ?>
  // on the other page...page2.php
  if (isset($_GET['process'])){ 
 $foo = $_GET[$vector]; // the array i want
 echo var_dump($foo);
  }

HTTP在设计上允许通过POST/GET进行数组。只需将相关项目名称相同,并以两个相对的方括号结尾,如下所示:

<input type="text" name="data[]" value"First"/>
<input type="text" name="data[]" value"Second"/>

在服务器上。。。

print_r($_REQUEST['data']);

打印。。。

Array
(
    [0] => First
    [1] => Second
)

很方便,嗯?

参数以数组形式传递。因此,您必须解析请求的变量

这是您的代码的修订版本。

<?php $vector = array("product1" => 150, "product2" => 120); ?>
  <table>
  <form action="page2.php" method="get">
  <?php foreach ($vector as $key => $value) { 
 echo "<tr><td>Product: $name</td><td><input type='text' name='$key' 
     value='$value'/>  
     </td>";
  }
  <tr>
<td><input type="submit" name="process" value="Submit" /></td>
  </tr>
  </form>
  </table>
  ?>
  // on the other page...page2.php
  if (isset($_GET['process'])){ 
    unset($_GET['process']);
    $foo = $_GET ;
    echo var_dump($foo);
 }

试着让它成为

$foo = $_GET ;

如果您不希望"进程"在阵列中,请首先调用

unset($_GET['process']);