【问题标题】:PHP returning very odd $_POST results when using form arrays使用表单数组时 PHP 返回非常奇怪的 $_POST 结果
【发布时间】:2011-07-18 22:11:44
【问题描述】:

是的,当用户提交表单以更新他们的联系信息时……奇怪的事情发生了,对我来说毫无意义,并且让我无法正确解析结果数据。

发送到我的脚本的$_POST 数据(通过print_r 找到)如下:

Array
(
    [name_first] => Charles
    [name_last] => Broughton
    [company] => 
    [address1] => 
    [address2] => 
    [city] => Bristol
    [region] => England
    [postal] => 
    [country] => 1
    [email] => *******************
    [phones_types] => Array
        (
            [0] => Cell
        )

    [phones_numbers] => Array
        (
            [0] => ************
        )

    [phone_types] => Array
        (
            [1] => Home
        )

    [phone_numbers] => Array
        (
            [1] => ************
        )

    [pass] => **********
    [id] => 
)

创建这个奇数输出的形式如下:

<form action="URL" method="POST">
  <table class="data" align="center" border="0" cellpadding="10" cellspacing="0" width="100%"><tbody>
    <tr>
      <th colspan="3">Edit Contact</th>
    </tr>
    <tr>
      <td>First Name:</td>
      <td><input type="text" name="name_first" value="Charles"/> (required)</td>
    </tr>
    <tr>
      <td>Last Name:</td>
      <td><input type="text" name="name_last" value="Broughton"/></td>
    </tr>
    <tr>
      <td>Company:</td>
      <td><input type="text" name="company" value=""/></td>
    </tr>
    <tr>
      <td>Address Line 1:</td>
      <td><input type="text" name="address1" value=""/></td>
    </tr>
    <tr>
      <td>Address Line 2:</td>
      <td><input type="text" name="address2" value=""/></td>
    </tr>
    <tr>
      <td>City:</td>
      <td><input type="text" name="city" value="Bristol"/> (required)</td>
    </tr>
    <tr>
      <td>Region:</td>
      <td><input type="text" name="region" value="England"/> (required)</td>
    </tr>
    <tr>
      <td>Postal:</td>
      <td><input type="text" name="postal" value=""/></td>
    </tr>
    <tr>
      <td>Country:</td>
      <td><select name="country">
        <option value="1" selected="selected">United Kingdom</option>
        <option value="2">United States</option>
      </select> (required)</td>
    </tr>
    <tr>
      <td>Email:</td>
      <td><input type="text" name="email" value="*******************"/> (required)</td>
    </tr>
    <tr>
      <td>Phones(s):</td>
      <td><input type="text" name="phones_types[0]" value="Cell"/>: <input type="text" name="phones_numbers[0]" value="************"/><br/><input type="text" name="phone_types[1]"/>: <input type="text" name="phone_numbers[1]"/></td>
    </tr>
    <tr>
      <td>Current Password:</td>
      <td><input type="password" name="pass"/> (required)</td>
    </tr>
    <tr align="center">
      <td colspan="2"><input type="submit" value="Save Changed"/></td>
    </tr>
    <input type="hidden" name="id" value=""/>
  </tbody></table>
</form>

有人知道我该如何解决这个问题,或者使用 PHP 正确解析它吗?我尝试了一个for 循环将两个数组解析为一个,但由于分离,它们都不是数组......上面的例子是print_r 输出。

--

编辑我尝试使用以下 PHP 代码解析表单数据,并输出错误消息。

if (count($_POST['phone_types'])!=0 && count($_POST['phone_numbers'])!=0)
{
  for ($i = 0; $i < count($_POST['phone_types']); $i++)
  {
    if (!empty($_POST['phone_types'][$i]) && !empty($_POST['phone_numbers'][$i]))
      $phones[$_POST['phone_types'][$i]] = $_POST['phone_numbers'][$i];
  }
  $ph = "";
  foreach ($phones as $k => $v)
  {
    $ph.= "$k:$v;";
  }
  $phones = $ph;
}
else
  $phones = "";

错误:

Warning: Invalid argument supplied for `foreach()` in `FILE` on line 35

【问题讨论】:

  • 你在期待什么?如果您不更改任何内容,这就是发布的内容
  • 我在提交时在表单中输入了第二个电话号码和第二个类型,而不是得到两个数组 [0] => 号码,[1] => 号码......他们是两个由于......表单输入顺序我猜是不同的两个数组集?但是后来PHP根本看不懂。
  • 我觉得很好:/有什么问题?
  • 您输入的命名错误(一个是单数,一个是单数) - 如果我理解您的问题...
  • @cdbroughton -- 使用[] 而不是[0], [1], ...

标签: php html forms for-loop


【解决方案1】:

您在输入字段中使用数组表示法:phones_types[0] 向 PHP 指示它应该创建一个 phones_types 数组,并且这个特定字段应该是其中的元素 0。

任何名称末尾带有[] 的表单字段都被PHP 视为将其视为数组的指令,因此您可以将多个值提交给同一个“名称”。

【讨论】:

  • 我知道这一点,但它正在创建单个数组 PER 索引,我猜是因为表单上的输入顺序。这意味着当我对其执行forforeach 时......输出没有两个索引,而是只有一个。
  • 那为什么不把它改成 [] 而不是索引版本呢?无论如何应该可以正常工作
【解决方案2】:
<input type="text" name="phones_types[0]" value="Cell"/>: 
<input type="text" name="phones_numbers[0]" value="************"/><br/>
<input type="text" name="phone_types[1]"/>: 
<input type="text" name="phone_numbers[1]"/>

应该是:

<input type="text" name="phones_types[0]" value="Cell"/>: 
<input type="text" name="phones_numbers[0]" value="************"/><br/>
<input type="text" name="phones_types[1]"/>: 
<input type="text" name="phones_numbers[1]"/>    

为了能够使用 foreach 循环电话类型和电话号码

【讨论】:

  • 哇,我不敢相信我没有看到。
【解决方案3】:

包含括号的字段名称会被 PHP 自动转换为数组。如果您遇到缺少数组条目的问题,请尝试将字段名称中的 [0] 更改为 []

http://www.php.net/manual/en/faq.html.php#faq.html.arrays

【讨论】:

  • 我知道这一点,但它正在创建单个数组 PER 索引,我猜是因为表单上的输入顺序。这意味着当我对其执行forforeach 时......输出没有两个索引,而是只有一个。
【解决方案4】:

你的一个帖子是给phones_numbers的,另一个是给phone_numbers

那些不匹配,所以它们将是单独的_POST

【讨论】:

    猜你喜欢
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2015-05-09
    • 2012-08-08
    相关资源
    最近更新 更多