简体   繁体   English

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

[英]Reading multiple textfield values using array in php

Can you please help me in this context. 在这种情况下,能否请您帮我。

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

I am using this code in a loop and I need to get all the values in another page. 我在循环中使用此代码,我需要在另一页中获取所有值。
How can I get these value as array in another page. 如何在另一个页面中将这些值作为数组获取。

you should be able to use $_POST array in the sample.php page 您应该能够在sample.php页面中使用$ _POST数组

PHP's $_POST Array, taken from the docs : PHP的$ _POST数组,取自docs

An associative array of variables passed to the current script via the HTTP POST method. 通过HTTP POST方法传递给当前脚本的变量的关联数组。

first, add s submit button to your form, it should look like this: 首先,在表单中添加s提交按钮,它应如下所示:

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

and in sample.php : 并在sample.php中

<?php
print_r($_POST);
?>

This is the most simple example, 这是最简单的例子

once you get the hang of it, I recommend you read the docs on topics such as: 一旦掌握了这些建议,我建议您阅读有关以下主题的文档

htmlspecialchars html特殊字符

htmlspecialchars — Convert special characters to HTML entities htmlspecialchars —将特殊字符转换为HTML实体

You can access it as array 您可以将其作为数组访问

$_POST['first'][0] // 0 will be index for single

to list all just loop 列出所有循环

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

Your values will end up in $_POST , and because you're using the [] syntax, you'll get an array. 您的值将以$_POST结尾,并且由于使用的是[]语法,因此将获得一个数组。 So, to get your first array, do: 因此,要获取第一个数组,请执行以下操作:

$first = $_POST['first'];

You could then iterate over it like so: 然后,您可以像这样迭代它:

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

I think this will help you. 我认为这会对您有所帮助。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM