php 检索帖子数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21750478/
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:25:38  来源:igfitidea点击:

Retrieve post array values

phpjqueryarrays

提问by StudentRik

I have a form that sends all the data with jQuery .serialize()In the form are four arrays qty[], etcit send the form data to a sendMail page and I want to get the posted data back out of the arrays.

我有一个使用 jQuery 发送所有数据.serialize()的表单在表单中有四个arrays qty[], etc它将表单数据发送到 sendMail 页面,我想将发布的数据从数组中取出。

I have tried:

我试过了:

$qty = $_POST['qty[]'];
foreach($qty as $value)
{
  $qtyOut = $value . "<br>";
}

And tried this:

并尝试了这个:

for($q=0;$q<=$qty;$q++)
{
 $qtyOut = $q . "<br>";
}

Is this the correct approach?

这是正确的方法吗?

回答by ajtrichards

You have []within your $_POSTvariable - these aren't required. You should use:

你有[]你的$_POST变量 - 这些不是必需的。你应该使用:

$qty = $_POST['qty'];

$qty = $_POST['qty'];

Your code would then be:

您的代码将是:

$qty = $_POST['qty'];

foreach($qty as $value) {

   $qtyOut = $value . "<br>";

}

回答by Thomas

php automatically detects $_POSTand $_GET-arrays so you can juse:

php 自动检测$_POST$_GET-arrays,所以你可以判断:

<form method="post">
    <input value="user1"  name="qty[]" type="checkbox">
    <input value="user2"  name="qty[]" type="checkbox">
    <input type="submit">
</form>

<?php
$qty = $_POST['qty'];

and $qtywill by a php-Array. Now you can access it by:

$qty将通过一个 php-Array。现在您可以通过以下方式访问它:

if (is_array($qty))
{
  for ($i=0;$i<size($qty);$i++)
  {
    print ($qty[$i]);
  }
}
?>

if you are not sure about the format of the received data structure you can use:

如果您不确定接收到的数据结构的格式,您可以使用:

print_r($_POST['qty']);

or even

甚至

print_r($_POST);

to see how it is stored.

看看它是如何存储的。

回答by radioSPARKS

My version of PHP 4.4.4 throws an error: Fatal error: Call to undefined function: size()

我的 PHP 4.4.4 版本抛出错误: 致命错误:调用未定义函数:size()

I changed sizeto countand then the routine ran correctly.

我将大小更改为计数,然后例程正确运行。

<?php
$qty = $_POST['qty'];

if (is_array($qty)) {
   for ($i=0;$i<count($qty);$i++)
   {
       print ($qty[$i]);
   }
}
?>

回答by Tailor993

I prefer foreach insted of for, because you do not need to heandle the size.

我更喜欢 foreach insted 的 for,因为你不需要处理大小。

if( isset( $_POST['qty'] ) )
{
    $qty = $_POST ['qty'] ;
    if( is_array( $qty ) )
    {
        foreach ( $qty as $key => $value ) 
        {
            print( $value );
        }
    }
}

回答by edmondscommerce

PHP handles nested arrays nicely

PHP 很好地处理嵌套数组

try:

尝试:

foreach($_POST['qty'] as $qty){
   echo $qty
}