Jake_D87
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.
And then on the following page i have the following PHP to retrieve the form variables.
All that, and i get nothing...
$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++;
}
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;
All that, and i get nothing...
on the following pageWe 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['particip ants']); helps
2) check if $participants = intval($_REQUEST['particip
ASKER
how do u do a var_dump?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
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> $partLastN ame[$i]</t d><td>$par tEmail[$i] </td><td>$ partType[$ i]</td></t r>";
Should be:
$participantBody .= "<tr><td>$partFirstName</t d><td>$par tLastName< /td><td>$p artEmail</ td><td>$pa rtType</td ></tr>";
Follow my two tips and that should work.
This:
$participantBody .= "<tr><td>$partFirstName[$i
Should be:
$participantBody .= "<tr><td>$partFirstName</t
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"/>";
<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"/>";
If you escape the embedded quotes it may work correctly:
echo "<input name='participants' type='hidden' id='participants' value=\"$participants\" />";
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:
process.php:
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.
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>
process.php:
<?php
foreach($_POST["magazines"] as $id = $magazineName)
{
echo "Person chose $magazineName<br />\n";
}
?>
?>
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.
$partFirstName = $_REQUEST['partFirstName[$
Try:
$partFirstName = $_REQUEST['partFirstName']
Same for the other variables.