rdrews Posted May 2, 2008 Share Posted May 2, 2008 I have a list of variable that are "C" plus a number that are sent by a form using the POST method. So, C0, C1, C2, C3, C4, .... It is NOT an array and the number of them created is dynamic so, obviously, I don't know how many there will be. My problem is this...I want to cycle through them using a while loop but I don't know what the syntax would be. I have: $i = 0 while(isset($_POST['$C$i'])){ ...do whatever... } This is not working. I have also tried while(isset($_POST["C"$i])) and probably a few other things. I just can't figure out what to put in the POST brackets. I am fairly new to PHP and like I said, I'm sure this is just something I am overlooking or there is probably a simple solution. Any help would be appreciated. Thanks, Ryan Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 2, 2008 Share Posted May 2, 2008 You should REALLY make it an array on the form (<input type="text" name="C[]" />), but I THINK you can do this: $i = 0 while(isset($_POST['C$i'])){ ...do whatever... $i++; } Not $C$i, jut C$i. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2008 Share Posted May 2, 2008 You'll need to remove the single quotes around the $i variable while(isset($_POST['C' .$i])){ or put the whole thing in double quotes - while(isset($_POST["C$i"])){ Evaluating an isset() in a while() statement will be exceedingly slow. Using the suggested array method will solve two problems, you can use a simple foreach() loop and if these are something like a checkbox/radio-button, where if one is not checked and won't be set, your loop won't stop at the first unchecked box/radio-button. Quote Link to comment Share on other sites More sharing options...
rdrews Posted May 2, 2008 Author Share Posted May 2, 2008 Thanks to both of you for the quick responses. Your help is appreciated but something is still just not working. Here are the two files I am working with to give a clearer explanation. H ere is my file1: <?php $gameDate = $_POST['gameDate']; ?> <form name="form" method="post" action="file2.php"> <?php $i = 0; while(isset($gameDate[$i])){ ?> <p>Enter comments for the <?php echo $gameDate[$i]; ?> game:<br /> <textarea name="C<?php echo $i; ?>" COLS=40 ROWS=6></textarea></p> <?php $i = $i + 1; } ?> <p><input type="submit" name="Submit" value="Submit" /></p> </form> ...all that is between the html and body tags. file2.php: <?php $i=0; while(isset($_POST['C' .$i])){ $test[$i] = $_POST['C' .$i]; $i++; } echo $test[0]; echo $test[1]; echo $test[2]; ?> In file1, the "gameDate" is data sent over from another form. For simplistic sake let's just say it just contains a number starting with 0 and ending wherever the user that submits the form sets. So...that textarea will be generated however many times the user has set in the previous form with the "gameDate" variable. I need each textarea name to be unique (or any array) so that I can access them on file2.php. I tried changing the name attribute for the textarea to "C[]" as suggested but could not get that to work either. I also tried the other methods mentioned to run through the C0, C1, C2, ... and couldn't get anything to work. Am I missing something here? I would like the textarea data to be saved to an array but I don't know how to do that within the form. Again, thanks for the quick responses. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2008 Share Posted May 2, 2008 The posted code works for me. When I set $_POST['gameDate'] to be an array with three values, it makes a form with three textareas and submits it and the echo $test[0]... code echos what I enter in the textareas. File1 assumes that $_POST['gameDate']; is an array. Is that so? Have you looked at the "view source" of the file1 form to make sure it is correct? Quote Link to comment Share on other sites More sharing options...
rdrews Posted May 6, 2008 Author Share Posted May 6, 2008 Thank you, it's all working now. My server was doing something weird caching and I wasn't seeing the changes I was making. Thanks for all your help! Quote Link to comment Share on other sites More sharing options...
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.