donglu5041 2016-10-04 13:45
浏览 133


I have a while loop creating a form, what I'm wanting the user to do is input a number next to the items they want, upon submitting it would submit both the number they have entered plus a variable I provide (without them seeing the variable). Is this possible? The php is looping through text files, outputting each line as an option for the user.

while(($line = fgets($filehandle)) !== false){
    $itemParts = explode(" - ",$line);
    $item = $itemParts[0];
    $price = $itemParts[1];
    echo "<input type='text' name='hardware[]' value='{$item}-|-{$price}'>£".$price." ".$item."<br />";

On the resulting page I am looping through the hardware[] array to display to the user their choices as well as updating a database.

foreach ($_POST['hardware'] as $itemWanted){
    $itemParts = explode('-|-',$itemWanted);
    $item = $itemParts[0];
    $cost = $itemParts[1];
    echo $item.' £'.$cost.'<br />';
    $total += $cost;
    $allHardware .= "**".$item." - GBP ".$cost;

I'm probably going about it the wrong way, but does anyone know if this is possible with an unknown number of text files and items? My current way of working is that there is a checkbox instead of the textbox and it all works OK, but now I need the users to choose an amount they require.

while(($line = fgets($filehandle)) !== false){
    $itemParts = explode(" - ",$line);
    $item = $itemParts[0];
    $price = $itemParts[1];
    echo "<input type='checkbox' name='hardware[]' value='{$item}-|-{$price}'><font size='2'>£".$price." ".$item."</font><br />";

Thanks in advance for any help

  • 写回答

1条回答 默认 最新

  • duanlan3598 2016-10-04 13:59

    You should try using input names as named arrays, it makes working with PHP loops much more fun:

      <input name='hardware[0][item]' value='{$item}'>
      <input type='hidden' name='hardware[0][price]' value='{$price}'>
      <input type='hidden' name='hardware[0][something]' value='{$something}'>

    in your php $_POST will be an array equivalent to :

     $_POST['hardware'] = array(
           0 => array(
                'item' => $item,
                'price' => $price,
                'something' => $something,

    you should replace 0 with an iterator if you're using multiple rows hope it helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 关于#ar#的问题:/* User can add his own implementation to report the HAL error return state */(语言-c语言)
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败