mlitch 0 Posted May 20, 2015 Report Share Posted May 20, 2015 My PHP-generated form file will generate any number of text fields called "recipient"+an integer, so "recipient1", "recipient2", etc The PHP file handling the form submission will not know how many fields there are, so I need to get something like the following logic: $i=1; $max=how many fields there are (it will know this from a mysqli_num_rows variable being passed) while ($i <= $max) { $recipient+$i = $_POST[recipient+$i] } In other words, as long as $i is less than or equal to the maximum number, generate a variable named "recipient" concatenated with $i and assign it the value of $_POST['recipient concatenated with $i']. I can't wrap my little brain around this. Help? Quote Link to post Share on other sites
Techneut 2 Posted May 20, 2015 Report Share Posted May 20, 2015 What is your problem actually? Because it seems you are not asking any question. Quote Link to post Share on other sites
mlitch 0 Posted May 20, 2015 Author Report Share Posted May 20, 2015 I need to write PHP code to handle a form. The form may have zero or more fields that are named "recipient1", "recipient2", "recipient3", etc. If there are two fields, I need to capture those fields. If there are six, I need to capture all six. Maybe there are none. I could write if (isset($_POST['recipient1'])) { $thisvariable1=$_POST['recipient1']; } if (isset($_POST['recipient2'])) { $thisvariable2=$_POST['recipient2']; } if . . . and so on. Maybe I could do 10 of these, but what if the form has 11 fields (recipient1 through recipient11)? I can't just write a hundred "if" statements. Surely there's some way to write a code that allows me to replace the "1", "2", and so on with some variable. The form itself will pass along how many fields there are. How can I use that number to automatically generate this kind of code that assigns the $_POST data to variables? Does that make more sense? How can I do this? That's my question. Quote Link to post Share on other sites
mlitch 0 Posted May 20, 2015 Author Report Share Posted May 20, 2015 (edited) To be more concrete, this code echo $_POST['recipient1']; echo '<br/>'; echo $_POST['recipient2']; echo '<br/>'; echo $_POST['recipient3']; echo '<br/>'; echo $_POST['recipient4']; echo '<br/>'; $i=1; while ($i<=$how_many) { ${"variable_$i"}=$_POST{"['recipient$i']"}; echo 'variable '.$i.' is '; echo ${"variable_$i"}; echo '<br/>'; $i=$i+1; } yields this: 612113variable 1 is variable 2 is variable 3 is variable 4 is Why can't I get the "while" loop to give me the same results as the statements before the "while" loop? (The variable $how_many equals 4 in this example.) Edited May 20, 2015 by mlitch Quote Link to post Share on other sites
mlitch 0 Posted May 20, 2015 Author Report Share Posted May 20, 2015 The problem is with how to use curly braces with the $_POST variable, because this code: $i=1; while ($i<=$how_many) { ${"variable_$i"}=1000+$i; echo 'variable '.$i.' is '; echo ${"variable_$i"}; echo '<br/>'; $i=$i+1; } yields (as I expect): variable 1 is 1001variable 2 is 1002variable 3 is 1003variable 4 is 1004 Quote Link to post Share on other sites
Ingolme 1,020 Posted May 20, 2015 Report Share Posted May 20, 2015 Why don't you use an array? That's what they're for. In the $_POST array you're supposed to wrap string indices in quotation marks. In PHP, the concatenation operator is ., not +. $recipient = array();while ($i <= $max) { $recipient[$i] = $_POST['recipient' . $i]} Quote Link to post Share on other sites
mlitch 0 Posted May 20, 2015 Author Report Share Posted May 20, 2015 Because I'm not good with arrays . . . but now that I see how to use it, I will! Thank you for showing me how to deal with the POST variable!!!!! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.