Variable number of Form inputs processed with PHP

Jake_D87
Jake_D87 used Ask the Experts™
on
So i am creating a form that has a input field on the previous page that asks the user how many peoples details they want to input on the next page.
$i = 1;
$participants = $_REQUEST['participants'];
echo "<input name='participants' type='hidden' id='participants' value=".$participants."/>";

while ($i <= $participants){
    echo "<tr>
      	         <td><input name='partFirstName[$i]' type='text' id='partFirstName[$i]' /></td>
      	         <td><input name='partLastName[$i]' type='text' id='partLastName[$i]' /></td>
      	         <td><input name='partEmail[$i]' type='text' id='partEmail[$i]' /></td>
      	         <td><select name='partType[$i]' id='partType[$i]'>
				<option selected='selected'>Please Select...</option>
				<option>Option 1</option>
				<option>Option 2</option>
			</select></td>
   	        </tr>";
  $i++;
}

Open in new window


And then on the following page i have the following PHP to retrieve the form variables.

$participants = $_REQUEST['participants'];
while ($i <= $participants){
	$partFirstName = $_REQUEST['partFirstName[$i]'];
	$partLastName = $_REQUEST['partLastName[$i]'];
	$partEmail = $_REQUEST['partEmail[$i]'];
	$partType = $_REQUEST['partType[$i]'];
	
	$participantBody .= "<tr><td>$partFirstName[$i]</td><td>$partLastName[$i]</td><td>$partEmail[$i]</td><td>$partType[$i]</td></tr>";
	$i++;
}
echo $participantBody;

Open in new window


All that, and i get nothing...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Instead of:
$partFirstName = $_REQUEST['partFirstName[$i]'];

Try:
$partFirstName = $_REQUEST['partFirstName'][$i];

Same for the other variables.
Most Valuable Expert 2011
Top Expert 2016

Commented:
on the following page
We may not have enough information to help you.  It would be good to see the entire scripts.  For example, in the second code snippet, the variable $i is undefined.

Suggest you add error_reporting(E_ALL) to the top of the scripts, too. It will help you catch typos and accidental reliance on undefined variables.

Commented:
1) try a var_dump of $_REQUEST to see what is being posted
2) check if $participants = intval($_REQUEST['participants']); helps

Author

Commented:
how do u do a var_dump?
I think you should generate field names like partFirstName_1, partFirstName_2 and so on.

Generate HTML like this:

$i = 1;
$participants = $_REQUEST['participants'];
echo "<input name='participants' type='hidden' id='participants' value=".$participants."/>";

while ($i <= $participants){
    echo "<tr>
                     <td><input name='partFirstName_$i' type='text' id='partFirstName_$i' /></td>
                     <td><input name='partLastName_$i' type='text' id='partLastName_$i' /></td>
                     <td><input name='partEmail_$i' type='text' id='partEmail_$i' /></td>
                     <td><select name='partType_$i' id='partType_$i'>
                        <option selected='selected'>Please Select...</option>
                        <option>Option 1</option>
                        <option>Option 2</option>
                  </select></td>
                 </tr>";
  $i++;
}
                                 

To Access posted data:

$participants = $_REQUEST['participants'];
while ($i <= $participants){
      $partFirstName = $_REQUEST['partFirstName_'$i];
      $partLastName = $_REQUEST['partLastName_'$i];
      $partEmail = $_REQUEST['partEmail_'$i];
      $partType = $_REQUEST['partType_'$i];
      
      $participantBody .= "<tr><td>$partFirstName</td><td>$partLastName</td><td>$partEmail</td><td>$partType</td></tr>";
      $i++;
}
echo $participantBody;
                                 

Let me know if any queries.
Or you can create array of HTML inputs and on server side read values from arrays.
Most Valuable Expert 2011
Top Expert 2016

Commented:
how do u do a var_dump?
Here is how I would do it.
echo "<pre>";
var_dump($_REQUEST);

Open in new window

If you are new to PHP and want to get some structured learning (instead of just reading code and guessing what the authors were trying to achieve) this is an excellent book.
http://www.sitepoint.com/books/phpmysql4/

Commented:
Oh, also:

This:
$participantBody .= "<tr><td>$partFirstName[$i]</td><td>$partLastName[$i]</td><td>$partEmail[$i]</td><td>$partType[$i]</td></tr>";

Should be:
$participantBody .= "<tr><td>$partFirstName</td><td>$partLastName</td><td>$partEmail</td><td>$partType</td></tr>";

Follow my two tips and that should work.

Commented:
Where are your

<form

and

</form

statements?

Your <input..... statement is not providing the number of participants correctly.

echo "<input name='participants' type='hidden' id='participants' value=".$participants."/>";

Remove the dots around the value.

echo "<input name='participants' type='hidden' id='participants' value="$participants"/>";
Most Valuable Expert 2011
Top Expert 2016

Commented:
This will cause a parse error:
echo "<input name='participants' type='hidden' id='participants' value="$participants"/>";

Open in new window

If you escape the embedded quotes it may work correctly:
echo "<input name='participants' type='hidden' id='participants' value=\"$participants\" />";

Open in new window

Commented:
While atique_asari's answer is technically valid, you are missing out on the ability to use POST/GET/REQUEST arrays in PHP. This might be fine when you KNOW the number of participants, you will probably run into scenarios later where you want to collect a dynamic number of inputs, like this:

form.html:
<form action='process.php' method='post'>
Pick your choices:
<input type='checkbox' name='magazines[]' value='Time' /> Time<br />
<input type='checkbox' name='magazines[]' value='Newsweek' /> Newsweek<br />
<input type='checkbox' name='magazines[]' value='Popular Science' /> Popular Science<br />
<input type='checkbox' name='magazines[]' value='Wall Street Journal' /> Wall Street Journal<br />
<input type='submit' value='Go!'>
</form>

Open in new window


process.php:
<?php
foreach($_POST["magazines"] as $id = $magazineName)
{
  echo "Person chose $magazineName<br />\n";
}
?>
?>

Open in new window


It's a very useful technique, so I would highly suggest that you reconsider using atique_asari's answer. You can still give him points, but don't discard using such a useful feature of PHP.

Now, one thing that I should say is that with inputs, NAME and ID attributes are different. NAME is the important one when you're submitting forms, because it is used to pass the name of the input to the page that processes the form data. If an input doesn't have a NAME, then it won't get passed to your form processor.

The ID attribute is primarily used for Javascripting. If you want to add Javascript to your page and have it manipulate form elements BEFORE you submit, then you need to uniquely identify each form element, and that is where ID comes in. However, Javascript doesn't interpret the [ ] brackets very well in the IDs of form elements, so if you do want to have dynamic form elements, the NAME attributes can use NAME='foo[1]' but the ID should look like ID='foo_1' so it's easier to reference in Javascript.
You are correct. I have also mentioned about the arrays. In the code above, we have no of participants and HTML inputs are generated for them. So both the solutions will be same.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial