Search code examples
phpformsdynamicform

Retrieving data from dynamic form


I have a form which is dynamically generated based on user input.

Initially the user is shown 3 fields to select from (datefrom, dateto, and pitchtype), they select their options, click submit and the script generates a table with inputs for each date in the range for them to edit.

An example of the generated form is below:

<table id="avtable">

    <thead>
        <tr>
            <th>Date</th>
            <th>Current bookings</th>
            <th>Max bookings</th>
        </tr>
    </thead>

    <tbody>

        <form action="index.php?page=availability&amp;task=edit&amp;action=submit" method="post"></form>

            <input type="hidden" name="pitchtype" value="1" />

            <tr>
                <td>2012-05-13</td>
                <td>0</td>
                <td><input type="text" value="10" class="spinner" maxlength="3" name="max"></td>
            </tr>

            <tr>
                <td>2012-05-14</td>
                <td>0</td>
                <td><input type="text" value="10" class="spinner" maxlength="3" name="max"></td>
            </tr>

            <input type="submit" value="Save" name="Submit" />

        </form>

    </tbody>

</table>

In the recieving PHP file I need to be able to process the data for each date, and am clueless where to start! Typically if it was a static form I would just use something like

$max = $_REQUEST['max'];

To get the edited field.

Obviously what I need to do is pair up each edited field with its date, for me to then process the data as I normally would. I presume I would need some sort of php loop with the array of data from the form, but I'm not quite sure where to start... any ideas?

EDIT :

I presume I would need to edit my outputed form to something like this, so the name of each input was the same as the date column, so what I need to know is then how to get all dates in the receiving php file:

<table id="avtable">

    <thead>
        <tr>
            <th>Date</th>
            <th>Current bookings</th>
            <th>Max bookings</th>
        </tr>
    </thead>

    <tbody>

        <form action="index.php?page=availability&amp;task=edit&amp;action=submit" method="post"></form>

            <input type="hidden" name="pitchtype" value="1" />

            <tr>
                <td>2012-05-13</td>
                <td>0</td>
                <td><input type="text" value="10" class="spinner" maxlength="3" name="2012-05-13"></td>
            </tr>

            <tr>
                <td>2012-05-14</td>
                <td>0</td>
                <td><input type="text" value="10" class="spinner" maxlength="3" name="2012-05-14"></td>
            </tr>

            <input type="submit" value="Save" name="Submit" />

        </form>

    </tbody>

</table>

Many thanks!

Kevin


Solution

  • In order to pass information first you need to give the text input's different names something like:

    <table id="avtable">
    
        <thead>
            <tr>
                <th>Date</th>
                <th>Current bookings</th>
                <th>Max bookings</th>
            </tr>
        </thead>
    
        <tbody>
    
            <form action="index.php?page=availability&amp;task=edit&amp;action=submit" method="post"></form>
    
                <tr>
                    <td>2012-05-13</td>
                    <td>0</td>
                    <td><input type="text" value="10" class="spinner" maxlength="3" name="SpinMax"></td>
                </tr>
    
                <tr>
                    <td>2012-05-14</td>
                    <td>0</td>
                    <td><input type="text" value="10" class="spinner" maxlength="3" name="SpinMax2"></td>
                </tr>
    
                <input type="submit" value="Save" name="Submit" />
    
            </form>
    
        </tbody>
    
    </table>
    

    From there, your PHP script should be something like this:

    $SpinMax = $_POST["SpinMax"];
    $SpinMax2 = $_POST["SpinMax2"];
    

    By using $_POST, you are able to assign the PHP variable based on the "name" you give it in the text area.