|
-
May 12th, 2007, 11:23 AM
#1
Thread Starter
PowerPoster
[RESOLVED] PHP generated forms
I have a form generated by PHP with an unknown number of textfields on this data is then posted to a script how can I loop through all of the text fields on the form?
Thanks
-
May 12th, 2007, 11:32 AM
#2
Re: PHP generated forms
If you give them all the same name followed by a pair of brackets [] PHP will automatically create an array for you containing all of the values.
PHP Code:
<form action="wherever" method="post">
<input type="text" name="values[]" value="1">
<input type="text" name="values[]" value="2">
<input type="text" name="values[]" value="3">
</form>
You can access the posted data using $_POST['values'][n].
If you cannot change the names of the text fields then you will have to loop through $_POST.
-
May 12th, 2007, 11:46 AM
#3
Thread Starter
PowerPoster
Re: PHP generated forms
Thanks pengate
How do I find the upper bound of the array?
-
May 12th, 2007, 11:51 AM
#4
Re: PHP generated forms
count($array) returns the length and the upper bound is 1 below that.
You can use either foreach() or for() to enumerate the array.
PHP Code:
foreach ($array as $value) { ... $value ... };
foreach ($array as $key => $value) { ... $value or $array[$key] ... };
for ($i = 0; $i < count($array); ++$i) { ... $array[$i] ... };
-
May 12th, 2007, 01:14 PM
#5
Thread Starter
PowerPoster
Re: [RESOLVED] PHP generated forms
For example I have the following,
PHP Code:
"<form name='form1' method='post' action='makeOrder.php'>";
//Then a number of input boxes.
<input name='quantity[]' type='text' size='6'>
For my example there is 3 boxes, I input data into them all and if I do this....
PHP Code:
$quantityArray[] = $_POST['quantity'];
Echo count($quantityArray);
I'm only getting the number 1 outputted!
Thanks
-
May 12th, 2007, 01:16 PM
#6
Thread Starter
PowerPoster
Re: [RESOLVED] PHP generated forms
Echo count($_POST['quantity']);
But that does seem to work, so my assignment to the array is wrong, hmm its agers since I've used php! What am I missing?
-
May 12th, 2007, 01:24 PM
#7
Re: [RESOLVED] PHP generated forms
The brackets are not needed:
PHP Code:
$quantityArray = $_POST['quantity'];
What the assignment $x[] = $y does is append $y to the end of the array $x. If $x does not already exist, $x will be created with $y as its sole offset; hence count($x) returns 1 and the actual array is being stored at $x[0].
Edit: I spent ages writing a reply to that other post, and then I couldn't figure out where it had gone when I previewed.
Last edited by penagate; May 12th, 2007 at 01:32 PM.
-
May 12th, 2007, 01:35 PM
#8
Thread Starter
PowerPoster
Re: [RESOLVED] PHP generated forms
Thanks, sorry I relised I was spamming in my random thoughts Thanks pengate helped a lot. I'll see if I cna finish this up now!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|