php 将表单值获取到php中的数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1924324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:19:46  来源:igfitidea点击:

get form values into array in php

php

提问by Gandalf StormCrow

Is it possible to get form field values into array? EX:

是否可以将表单字段值放入数组?前任:

 <?php

   array('one', 'two', 'three');    
    ?>

    <form method="post" action="test.php">
        <input type="hidden" name="test1" value="one" />
        <input type="hidden" name="test2" value="two" />
        <input type="hidden" name="test3" value="three" />
        <input type="submit" value="Test Me" />
    </form>

So is it possible to pass all form values no matter the number of them to the array in php ?

那么是否可以将所有表单值传递给 php 中的数组,而不管它们的数量?

回答by Xeoncross

Yes, just name the inputs the same thing and place brackets after each one:

是的,只需将输入命名为相同的内容并在每个输入后放置括号:

<form method="post" action="test.php">
        <input type="hidden" name="test[]" value="one" />
        <input type="hidden" name="test[]" value="two" />
        <input type="hidden" name="test[]" value="three" />
        <input type="submit" value="Test Me" />
</form>

Then you can test with

然后你可以测试

<?php
print_r($_POST['test']);
?>

回答by Daniel A. White

It already is done.

它已经完成了。

Look at the $_POSTarray.

看看$_POST数组。

If you do a print_r($_POST);you should see that it is an array.

如果你做 aprint_r($_POST);你应该看到它是一个数组。

If you just need the values and not the key, use

如果您只需要值而不是键,请使用

$values = array_values($_POST);

http://php.net/manual/en/reserved.variables.post.php

http://php.net/manual/en/reserved.variables.post.php

回答by zombat

This is actually the way that PHP was designed to work, and one of the reasons it achieved a large market penetration early on with web programming.

这实际上是 PHP 被设计的工作方式,也是它在 Web 编程早期实现大规模市场渗透的原因之一。

When you submit a form to a PHP script, all the form data is put into superglobal arrays that are accessible at any time. So for instance, submitting the form you put in your question:

当您向 PHP 脚本提交表单时,所有表单数据都被放入可随时访问的超全局数组中。例如,提交您在问题中提交的表格:

<form method="post" action="test.php">
    <input type="hidden" name="test1" value="one" />
    <input type="hidden" name="test2" value="two" />
    <input type="hidden" name="test3" value="three" />
    <input type="submit" value="Test Me" />
</form>

would mean that inside test.php, you would have a superglobal named $_POSTthat would be prefilled as if you had created it with the form data, essentially as follows:

这意味着在 inside 中test.php,您将拥有一个名为 superglobal 的超全局变量$_POST,它会被预填充,就像您使用表单数据创建它一样,基本上如下所示:

$_POST = array('test1'=>'one','test2'=>'two','test3'=>'three');

There are superglobals for both POST and GET requests, ie. $_POST, $_GET. There is one for cookie data, $_COOKIE. There is also $_REQUEST, which contains a combination of all three.

POST 和 GET 请求都有超全局变量,即。$_POST, $_GET. 有一个用于 cookie 数据,$_COOKIE. 还有$_REQUEST,它包含所有三个的组合。

See the doc page on Superglobalsfor more info.

有关更多信息,请参阅Superglobals 上文档页面