dongwaner1367 2014-01-28 10:47
浏览 15
已采纳

如何以编程方式检索任意数量的类似命名的post变量

Before getting to my registration page, my users specify te number (X) of group members to be registered. Upon arriving on that page, X registration forms are generated, one for each member. So I have a first_name1 text field for the first person, then first_name2, first_name3, etc. In total, X first_name fields each with a number at the end of it.

Problem is when retrieving those variables. There can be 20 people, so in my code I won't do:

$_POST['first_name1'] 
$_POST['first_name2'] 
...
$_POST['first_name20'] 

Instead, I'd like to do something like:

$_POST['first_name.$number'] 

Is it possible in PHP?

  • 写回答

3条回答 默认 最新

  • doutouman6245 2014-01-28 10:50
    关注

    You can use arrays in your form element names like this:

    <input name="first_name[]" type="text" />
    <input name="first_name[]" type="text" />
    <input name="first_name[]" type="text" />
    

    In your php you can then access it by iterating over the array:

    foreach($_POST['first_name'] as $index => $first_name) {
      // Do something with $first_name
    }
    

    Note: you can create very complex structures using this method, this isn't just limited to the simple example above. You can even create a multidimensional array, for example:

    <input name="user[0][first_name]" type="text" />
    <input name="user[0][last_name]" type="text" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比