Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Web Design & Development Web Design & Development
Search Search
Search for:
Tech Support Guy > > >

Solved: Capturing POST Info from varying amounts of form fields


(!)

Swanny86uk's Avatar
Swanny86uk Swanny86uk is offline
Computer Specs
Member with 52 posts.
THREAD STARTER
 
Join Date: Dec 2006
Experience: Intermediate
01-Jul-2011, 09:08 AM #1
Solved: Capturing POST Info from varying amounts of form fields
Hey,

I'm having a little trouble thinking about how I return values from a form where the number of fields is not always going to be the same.

The first step of my form allows the user to select a number between 1 and 50, once they have done this the page reloads with the number of fields they selected and done this via this code;

PHP Code:
for ($i 1$i <= $fields$i++) {
    echo 
"<label>Variable $i</label>&nbsp;&nbsp;<input type='text name='$i'><br /><br />";

The problem I have with this is that on the next page where I want to use the values from these form fields I can't think of how to capture them as I don't know how many will be coming through. I could do an absolutely massive IF statement but that seems a bit stupid.

Does anyone know how I could do this?
tomdkat's Avatar
Computer Specs
Trusted Advisor with 7,141 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
01-Jul-2011, 10:35 AM #2
In your script, when you send back the page with the list of form fields, also send back a hidden form field that contains the number of fields that were generated. Then, when the user submits the form with the generated form fields on it, the number of generated fields is sent back also.

The hidden form field would look like this:
Code:
<input type="hidden" value="{number of generated fields}" name="generatedFieldCount" />
Then, when the form is sent back (with the generated fields filled in), you can get the number of generated fields by getting the value of the "generatedFieldCount" form field and use that to control a for loop to process the fields.

Peace...
Swanny86uk's Avatar
Swanny86uk Swanny86uk is offline
Computer Specs
Member with 52 posts.
THREAD STARTER
 
Join Date: Dec 2006
Experience: Intermediate
06-Jul-2011, 09:58 AM #3
Thanks again,

I seem to be struggling a bit on the loop section of this however. My code for the loop is below - the $fields variable is the number of form fields submitted.

PHP Code:
$fields $_POST['fields'];

for(
$i 1$i <= $fields$i++) {
        
$i $_POST[$i];
        echo 
$i;

Each form field name is a number from 1-50 which is why I'm using the $_POST[$i] but I keep getting an offset error :S
tomdkat's Avatar
Computer Specs
Trusted Advisor with 7,141 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
06-Jul-2011, 10:52 AM #4
Try using a variable OTHER than $i to both hold the data from the form AND act as the control of the for() loop. Maybe this:

Code:
$fields = $_POST['fields'];

for($i = 1; $i <= $fields; $i++) {
        $fieldValue = $_POST[$i];
        echo $fieldValue;
}
Peace...
Swanny86uk's Avatar
Swanny86uk Swanny86uk is offline
Computer Specs
Member with 52 posts.
THREAD STARTER
 
Join Date: Dec 2006
Experience: Intermediate
06-Jul-2011, 11:37 AM #5
Excellent that works - I had also forgot to add a final ' on my form on the previous page which was why the values weren't coming through, all solved now though,

Thanks!
tomdkat's Avatar
Computer Specs
Trusted Advisor with 7,141 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
06-Jul-2011, 12:10 PM #6
Great!

Peace...
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


Tags
for loop, form field, forms, php, post data

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑