drxkx6149 2010-12-13 03:24
浏览 15
已采纳

如何获取多值多字段

i have a form and I repeate the same fields because i don't the user if he has more than one Item to send the first Item and he do agin and agin i want it from the first time to add the all items he has, so i put the same Required fields more than once.

<form>
    <select name="items[]">
    <option value="clothes">Clothes</option>
    <option value="shoes">Shoes</option>
    </select>
    <input type="text" name="color[]" />

    <select name="items[]">
    <option value="clothes">Clothes</option>
    <option value="shoes">Shoes</option>
    </select>
    <input type="text" name="color[]" />
</form>

and I know how to get just one field like this : name="color[]" I can get the result with:

while(list($key,$value) = each($_POST['color'])){

if(!empty($value)){

echo $_POST['color'][$key];
echo "<br />" ;

}}

but now i want to get all values "select items and color"

i made this code here and I got all the results But it is not linked to each other!!!

like this: clothes shoes red blue

i want it like this: clothes-red shoes-blue

this is the code which i made But I'm not satisfied

while(list($key,$value) = each($_POST['input'])){

if(!empty($value)){

echo $_POST['input'][$key];
echo "<br />" ;

}}

while(list($key,$value) = each($_POST['items'])){

if(!empty($value)){

echo $_POST['items'][$key];
echo "<br />" ;

}}

can anybody help me please :)

thank you,

  • 写回答

2条回答 默认 最新

  • dongtaihui5131 2010-12-13 03:29
    关注

    Change your select and input names to be like this

    <select name="items[0][type]">
    <input name="items[0][color]">
    
    <select name="items[1][type]">
    <input name="items[1][color]">
    

    Then you can iterate each pair together using

    foreach ($_POST['items'] as $item) {
        $type = $item['type'];
        $color = $item['color'];
    }
    

    Edit Forgot to add an index to each pair to group them together

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥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的三轴机械手程序