Re: dynamic array creation from form elements

From: Date: Fri, 16 Jan 2004 15:47:08 +0000
Subject: Re: dynamic array creation from form elements
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-174836@lists.php.net to get a copy of this message
Jon Bennett wrote:
Hi, I've got a dynamically generated form and once it's submitted I need to _push the data from my form elements into an array so I can ad them to the db. My form elements are created like so: <?php $i = 1; while ($i <= $iListItems){ ?> <div class="row"> <div class="label">List Item <?php print $i ?>:</div><div class="formw"><input type="text" name="listItem<?php print $i ?>" size="40" /></div> </div>
Why don't you just skip the whole "$i" counter thing and name your input elements as "listItem[]" ? Name every one of them that; the same thing. Now when you submit your form, you'll already have an array, $_POST['listItem']. $_POST['listItem'][0] will be what was in the first one, $_POST['listItem'][1] in the second, etc. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com

« previous php.general (#174836) next »