Jump to content

Using PHP arrays in a HTML form


paultaylor01

Recommended Posts

Hi,

 

I want to use a PHP array in an HTML. I want 10 rows to display in the form, and each row contains the same fields. Rather than coding the same row 10 times, and then having laborious php code to process each row in tutn, I though I would use an array. I want the <input name...> to be an element of the array.

 

<form method='post'>
<?php
for ($i=1;$i<=10;$i++)
{
 ?>
  Item: <input type='text' name='<?php echo $item_code[$i]' ?>' size='12' maxlength='12'>
  Qty: <input type='text' name='<?php echo $item_quantity[$i] ?>' size='11' maxlength='11'>
  Unit Price: <input type='text' name='<?php echo $item_price[$i] ?>' size='11' maxlength='11'>
  Vat Rate: <input type='text' name='<?php echo $vat_rate[$i] ?>' size='8' maxlength='8'><br>
 <?php
}
?>
</form>

 

I'm pretty sure the "echo" is wrong, but I'm not sure how to code each name to be an array element. Is this possible?

 

TIA

 

Paul

Link to comment
https://forums.phpfreaks.com/topic/97637-using-php-arrays-in-a-html-form/
Share on other sites

<?php

if(isset($_POST['submit']))
{
    echo '<pre>' . print_r($_POST, true) . '</pre>';
}
?>
<form method="post">
<table border="0" cellspacing="2" cellpadding="4">
  <tr>
    <th>Item</th>
    <th>Qty</th>
    <th>Unit Price</th>
    <th>Vat Rate</th>
  </tr>
<?php for ($i=1;$i<=10;$i++): ?>
  <tr>
    <td><input type="text" name="item_code[]" size="12" maxlength="12"></td>
    <td><input type="text" name="item_quantity[]" size="11" maxlength="11"></td>
    <td><input type="text" name="item_price[]" size="11" maxlength="11"></td>
    <td><input type="text" name="item_rate[]" size="8" maxlength="8"></td>
  </tr>
<?php endfor; ?>
</table>
<input type="submit" name="submit" value="submit" />
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.