Link to home
Start Free TrialLog in
Avatar of Jake_D87
Jake_D87Flag for Australia

asked on

Variable number of Form inputs processed with PHP

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...
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

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

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

Same for the other variables.
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.
1) try a var_dump of $_REQUEST to see what is being posted
2) check if $participants = intval($_REQUEST['participants']); helps
Avatar of Jake_D87

ASKER

how do u do a var_dump?
ASKER CERTIFIED SOLUTION
Avatar of Atique Ansari
Atique Ansari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or you can create array of HTML inputs and on server side read values from arrays.
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/
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.
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"/>";
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

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.