1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Capturing POST Info from varying amounts of form fields

Discussion in 'Web Design & Development' started by Swanny86uk, Jul 1, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. Swanny86uk

    Swanny86uk Thread Starter

    Joined:
    Dec 1, 2006
    Messages:
    52
    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:
    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?
     
  2. tomdkat

    tomdkat Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,141
    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...
     
  3. Swanny86uk

    Swanny86uk Thread Starter

    Joined:
    Dec 1, 2006
    Messages:
    52
    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:
    $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
     
  4. tomdkat

    tomdkat Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,141
    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++) {
            $[b]fieldValue[/b] = $_POST[$i];
            echo $[b]fieldValue[/b];
    }  
    
    Peace...
     
  5. Swanny86uk

    Swanny86uk Thread Starter

    Joined:
    Dec 1, 2006
    Messages:
    52
    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!
     
  6. tomdkat

    tomdkat Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,141
    Great!

    Peace...
     
  7. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1005171