paultaylor01 Posted March 24, 2008 Share Posted March 24, 2008 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 More sharing options...
wildteen88 Posted March 24, 2008 Share Posted March 24, 2008 <?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 https://forums.phpfreaks.com/topic/97637-using-php-arrays-in-a-html-form/#findComment-499576 Share on other sites More sharing options...
paultaylor01 Posted March 24, 2008 Author Share Posted March 24, 2008 Wonderful, worked a treat! Thanks Link to comment https://forums.phpfreaks.com/topic/97637-using-php-arrays-in-a-html-form/#findComment-499582 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.