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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.