donglu5041 2016-10-04 13:45
浏览 133
已采纳

html文本输入发送php变量以及用户的输入

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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用