HTML文本输入发送一个PHP变量以及用户的输入


html text input sending a php variable as well as the user's input

我有一个while循环创建一个表单,我想让用户做的是在他们想要的项目旁边输入一个数字,在提交时,它会提交他们输入的数字加上我提供的变量(没有他们看到变量)。这可能吗?php正在遍历文本文件,输出每一行作为用户的选项。

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 />";
}

在生成的页面上,我循环遍历hardware[]数组,向用户显示他们的选择以及更新数据库。

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

我可能走错了路,但是有没有人知道这是否可能与未知数量的文本文件和项目?我目前的工作方式是有一个复选框而不是文本框,它都工作正常,但现在我需要用户选择他们需要的金额。

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 />";
}

提前感谢您的帮助

您应该尝试使用输入名称作为命名数组,这使得使用PHP循环更有趣:

  <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}'>

在你的php $_POST将是一个数组相当于:

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

如果使用多行,应该用迭代器替换0希望能有所帮助:)