I have a form that allows users to register into a database. They can register up to a group of 6 at a time. Currently the webpage displays two forms. The first (userNumberForm) is just a drop down list where the user picks how many names to register. That gets posted and when the page reloads the 2nd form (userInfoForm) runs a while loop to populate the correct number of fields for the user to enter their information in. It also contains a hidden field where the value from the userNumberForm is stored. When the userInfoForm gets posted the first thing I have the addUser.php (the page it posts to) do is check the number of users and store that in a variable called userNum. Currently I have a switch statement based on that variable to get the rest of the information from the form and sanitize the data.
case 2:
$FirstName1=$_POST['FirstName1'];
$FirstName2=$_POST['FirstName2'];
$FirstName1 = stripslashes($FirstName1);
$FirstName1 = mysql_real_escape_string($FirstName1);
$FirstName2 = stripslashes($FirstName2);
$FirstName2 = mysql_real_escape_string($FirstName2);
break;
case 3:
$FirstName1=$_POST['FirstName1'];
$FirstName2=$_POST['FirstName2'];
$FirstName3=$_POST['FirstName3'];
$FirstName1 = stripslashes($FirstName1);
$FirstName1 = mysql_real_escape_string($FirstName1);
$FirstName2 = stripslashes($FirstName2);
$FirstName2 = mysql_real_escape_string($FirstName2);
$FirstName3 = stripslashes($FirstName3);
$FirstName3 = mysql_real_escape_string($FirstName3);
break;
//... and so on
As you can see this is starting to be a lot of code. I am hoping there is a way to set something up like this:
$x = 1;
while($x < $userNum)
{
$FirstName.$X=$_POST['FirstName.$X'];
$FirstName.$X = stripslashes($FirstName.$X);
$FirstName.$X = mysql_real_escape_string($FirstName.$X);
++$x;
}
Obviously that doesn't work, but that is the format I am looking for. I don't fully understand variable variables yet, Or maybe I'm making this more complicated than it needs to be and there is a way to stick this in an array.