在php中将表单值转换为数组

Gan*_*row 3 php

是否可以将表单字段值转换为数组?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>
Run Code Online (Sandbox Code Playgroud)

那么有可能将所有表单值传递给php中的数组,无论它们的数量是多少?

Dan*_*ite 12

它已经完成了.

看看$_POST数组.

如果你这样做,print_r($_POST);你应该看到它是一个数组.

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

$values = array_values($_POST);
Run Code Online (Sandbox Code Playgroud)

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


Xeo*_*oss 12

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

<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>
Run Code Online (Sandbox Code Playgroud)

然后你可以测试

<?php
print_r($_POST['test']);
?>
Run Code Online (Sandbox Code Playgroud)


zom*_*bat 6

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

当您向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>
Run Code Online (Sandbox Code Playgroud)

意味着在里面test.php,你会有一个超级全局名称$_POST,它将被预先填充,就像你用表单数据创建它一样,基本上如下:

$_POST = array('test1'=>'one','test2'=>'two','test3'=>'three');
Run Code Online (Sandbox Code Playgroud)

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

有关详细信息,请参阅Superglobals上doc页面.