dongmanni6916 2014-08-22 17:27
浏览 47
已采纳

在具有变量ID的php中捕获POST数据

I'm creating a form from a database and the input id's could be 1-9, 1,2,5,8, etc. IE with the way it is now, I cannot determine what the number will be unless I were to iterate from number 1 to the final number of menu items in the database... which I imagine is not optimal from a coding perspective.

I have two files. File1 will get list number of menu items from a database and create a list. The condensed version of my code is as follows, please keep in mind i have condensed a lot of useless stuff;

File1.php

    $menuArray = openMenu(1);
    $return = "<div id='menu'><form method='post' action='file2.php'><input type='submit' name='submit' value='Commit Order' /><table class='tableinfo'>";
    $i=1;
    foreach($menuArray as $recordNum => $record)
    {
            if ($record['available'] > 0)
            {
                    $thisClass='available';
            } else{
                    $thisClass='unavailable';
            }

            $return.="<tr class='$thisClass'>
                            <td>$record[itemid]</td>
                            <td><label for='$record[itemid]'>$record[name]</label></td>
                            <td><button type='button' id='itemid-$record[itemid]' class='subtract'>-</button><input class='itemadder' id='itemid-$record[itemid]' type='number' min='0' value='0' /><button id='itemid-$record[itemid]' class='addition' type='button'>+</button></td>
                    </tr>";
    }
    $return.="</table></form></div>";

    return $return;

File2.php I don't know how to code this :(

Is anyone able to shed some light on the best way to do this? I just need a way to be able to see what id's have a value when posted.

I am using jQuery at the moment; would this be something best done using jquery?

  • 写回答

2条回答 默认 最新

  • douwei3172 2014-08-22 17:36
    关注

    Assuming I understand you correctly the best way to do this would be to have an array of inputs.

    Code you should try to achieve for your HTML output would need to be something like this:

    <input type="text" name="number[1]" value="" />
    <input type="text" name="number[3]" value="" />
    <input type="text" name="number[5]" value="" />
    

    Now you know after your form submission in PHP:

    foreach($_POST['number'] as $id => $value){
        echo 'The value for ID #' . $id . ' is ' . $value; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧