Maff Maff - 1 year ago 64
HTML Question

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

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

Answer Source

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 :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download