Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/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
在php中使用数组读取多个文本字段值_Php_Html_Forms - Fatal编程技术网

在php中使用数组读取多个文本字段值

在php中使用数组读取多个文本字段值,php,html,forms,Php,Html,Forms,你能在这方面帮助我吗 <form action="sample.php" method="post"> <input type="text" name="first[]" /> <input type="text" name="second[]" /> </form> 我在循环中使用此代码,需要在另一个页面中获取所有值。 如何在另一个页面中以数组的形式获取这些值。您应该能够在sample.php页面中使用$\u POST数组 PHP的$\u

你能在这方面帮助我吗

<form action="sample.php" method="post">
<input type="text" name="first[]" />
<input type="text" name="second[]" />
</form>

我在循环中使用此代码,需要在另一个页面中获取所有值。

如何在另一个页面中以数组的形式获取这些值。

您应该能够在sample.php页面中使用$\u POST数组

PHP的$\u POST数组,取自:

通过HTTP POST方法传递到当前脚本的变量的关联数组

首先,将s submit按钮添加到表单中,它应该如下所示:

<form action="sample.php" method="post">
  <input type="text" name="first[]" />
  <input type="text" name="second[]" />
  <input type="submit"> 
</form>

sample.php中:

<?php
print_r($_POST);
?>

这是最简单的例子

一旦你掌握了窍门,我建议你阅读以下主题:

htmlspecialchars-将特殊字符转换为HTML实体


您可以作为数组访问它

$_POST['first'][0] // 0 will be index for single
列出所有的循环

foreach($_POST['first'] as $first) {
     ...
}

我想这对你有帮助

 $first=$_POST['first'];
  $second=$_POST['second'];
  foreach($first as $key=>$first){

   echo 'Your first value'.$first.'Your second value'.$second[$key];
}

您的值将在
$\u POST
中结束,因为您使用的是[]语法,所以您将得到一个数组。因此,要获得第一个阵列,请执行以下操作:

$first = $_POST['first'];
然后您可以像这样对其进行迭代:

foreach ($first as $item) {
    ...
}

那么您也可以循环
表单
?只需创建一个表单并将所有生成的
输入
放入其中。“另一个页面?”这是
sample.php
还是完全不同的页面?在这个例子中,
first[]
second[]
的意义是什么?我已经在下面编辑了我的答案,如果它对你有帮助,请接受。你能举个例子吗