【问题标题】:Passing dynamic input field array item values with different names to php将具有不同名称的动态输入字段数组项值传递给php
【发布时间】:2015-12-20 22:55:17
【问题描述】:

我正在使用来自formvalidation.io的这个表格:

<form id="bookForm" method="post" class="form-horizontal">
<div class="form-group">
    <label class="col-xs-1 control-label">Book</label>
    <div class="col-xs-4">
        <input type="text" class="form-control" name="book[0].title" placeholder="Title" />
    </div>
    <div class="col-xs-4">
        <input type="text" class="form-control" name="book[0].isbn" placeholder="ISBN" />
    </div>
    <div class="col-xs-2">
        <input type="text" class="form-control" name="book[0].price" placeholder="Price" />
    </div>
    <div class="col-xs-1">
        <button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i></button>
    </div>
</div>

输入字段名称非常特殊,例如name="book[0].title"

如何将所有这些输入字段数组值传递给我的 php 脚本?

我猜"." 不符合 php。以name="book[0][title] 之类的方式更改名称会导致出现错误消息,例如Warning: Illegal string offset 'title' in www/...print_r($_POST['book'][0]['title']);

感谢您的帮助!

【问题讨论】:

    标签: php arrays forms


    【解决方案1】:

    您必须改用print_r($_POST[books][0][title]);,希望对您有所帮助。 另外,它是“书”还是“书”?这也可能带来错误。但我的意思是,您不必使用 ' 分隔符。

    【讨论】:

    • 抱歉我的错字!这是“书”。
    • 如何在php中循环这种类型的输入数组值?
    • 类似foreach($_POST[book] as $myBook){ ... }
    【解决方案2】:

    以下格式应该可以工作

    <input type="text" class="form-control" name="book[0][title]" placeholder="Title" />
    

    当您print_r($_POST) 时,您将以正确的数组格式获得提交的数据。也可以通过$_POST['book'][0]['title']获取标题

    [book] => Array
            (
                [0] => Array
                    (
                        [title] => titletext
                        [isbn] => isbntext
                        [price] => 100
                    )
    
            )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 2011-11-20
      相关资源
      最近更新 更多