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
Printable View
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
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.
You can access the posted data using $_POST['values'][n].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>
If you cannot change the names of the text fields then you will have to loop through $_POST.
Thanks pengate
How do I find the upper bound of the array?
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] ... };
For example I have the following,
For my example there is 3 boxes, I input data into them all and if I do this....PHP Code:"<form name='form1' method='post' action='makeOrder.php'>";
//Then a number of input boxes.
<input name='quantity[]' type='text' size='6'>
I'm only getting the number 1 outputted!PHP Code:$quantityArray[] = $_POST['quantity'];
Echo count($quantityArray);
Thanks
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?
The brackets are not needed:
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].PHP Code:$quantityArray = $_POST['quantity'];
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. :)
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!