Jump to content

[SOLVED] Dynamic Form Fields


lukkyjay

Recommended Posts

I tried to make some good advice from Barand in this thread (http://www.phpfreaks.com/forums/index.php/topic,150773.0.html) work on a similar but different problem I have, but got confused because I'm not so smart.  :-[

What I'm trying to do is have the user say how many employees are in the company and hit "Continue."  Then the next page will pull up a form with the same number of fields as what they entered so they can give the information for each employee.  However, I can't figure out how to give each employee a separate variable.  If they say "7" employees, there are 7 rows that pull up but they all use the same variables.  Here is the code:

*first page

<form id="form1" name="form1" method="post" action="test3.php">
      <label for="numberEmployees">Number of eligible employees and owners:</label>
      
        <select name="numberEmployees[]" size="1" id="numberEmployees[]">
            <option value="1" selected="selected">1</option>
            <option value="2">2</option>
            ...
            <option value="50">50</option>
             </select>
            <p>
        <input type="submit" name="Continue" id="Continue" value="Continue" />
        </p>
    </form>

 

*and the second page

 

<?php  
if (isset($_POST['numberEmployees']))
{
    foreach ($_POST['numberEmployees'] as $k=>$employee)
    {
        $employeeName = $_POST['employeeName'][$k];         // get other fields for same input set
        $employeeGender = $_POST['employeeGender'][$k];
        $employeeZip = $_POST['employeeZip'][$k];
        $employeeDOB = $_POST['employeeDOB'][$k];
        $spouseDOB = $_POST['spouseDOB'][$k];
	$children = $_POST['children'][$k];
        
    }
}
      echo '<table width="200" border="1">';
echo '<tr>
          <td>Employee Name </td>
          <td>Gender</td>
          <td>Home Zip Code </td>
          <td>Date of Birth<br />
            mm/dd/yyyy</td>
          <td>Spouse Date of Birth<br />
          mm/dd/yyyy</td>
          <td>Children? </td>
        </tr>';

        
for ($i=0; $i<$employee; $i++)
{
    echo '<tr> 
          <td><textarea name="employeeName[]" id="employeeName[]"></textarea></td>
          <td><select name="employeeGender[]" id="employeeGender[]">
            <option value="male" selected="selected">Male</option>
            <option value="female">Female</option>
          </select>
          </td>
          <td><input name="employeeZip[]" type="text" id="employeeZip[]" size="7" /></td>
          <td><input name="employeeDOB[]" type="text" id="employeeDOB[]" size="12" /></td>
          <td><input name="spouseDOB[]" type="text" id="spouseDOB[]" value="N/A" size="12" /></td>
          <td><select name="children[]" id="children[]">
            <option value="no" selected="selected">No</option>
            <option value="yes">Yes</option>
          </select>
          </td>
        </tr>';
}     
echo '</table>';
        echo '<input type="submit" name="Submit" value="Submit" />';
echo '</form>'
?>

 

I know it's probably real simple and I didn't want to ask, but can anybody tell me what I need?  Thanks.

Link to comment
Share on other sites

On the first page the number of employees will be a single field, not an array, so the select name should not have the "[]"

 

On second page you just need

$employee = $_POST['numberEmployees']

followed by the code to produce the table rows X times.

 

The code you have at the top should be in the next (3rd) page when the multiple data is submitted

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.