morraine Posted April 21, 2008 Share Posted April 21, 2008 hello, im trying to process a form which has 10 identical sections with some kind of loop. the form is like <INPUT type=checkbox value=Yes name=fieldcheck1> <INPUT size=42 name=name1> <INPUT size=42 name=phone1> <INPUT size=42 name=address1> <INPUT type=checkbox value=Yes name=fieldcheck2> <INPUT size=42 name=name2> <INPUT size=42 name=phone2> <INPUT size=42 name=address2> <INPUT type=checkbox value=Yes name=fieldcheck3> <INPUT size=42 name=name3> <INPUT size=42 name=phone3> <INPUT size=42 name=address3> and on to like this 10 times now i m trying to colect the data from each section of the form with a loop that will check the check box and if its YES then it will insert the data for the section into the database and then increment the varibles number and do the same for the next section until it gets to the end with no sections left to save to database. does any one know how to setup a while loop to do this? Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 21, 2008 Share Posted April 21, 2008 try something like this <?php for($i = 1; $i <= 10; $i++) { if ($isset($_POST["fieldcheck" . $i])) { // Get the data and process it $name = $_POST["name" . $i]; // etc } } ?> Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted April 21, 2008 Share Posted April 21, 2008 A for loop is easier: for ($i = 1; $i <= 10; $i++) { echo $_POST['name' . $i]; } However, it is probably even easier to use a different method of gathering the data: <INPUT type=checkbox value=Yes name="userdata[1][checkbox]"> <INPUT size=42 name="userdata[1][name]"> <INPUT size=42 name="userdata[1][phone]"> <INPUT size=42 name="userdata[1][address]"> <INPUT type=checkbox value=Yes name="userdata[2][checkbox]"> <INPUT size=42 name="userdata[2][name]"> <INPUT size=42 name="userdata[2][phone]"> <INPUT size=42 name="userdata[2][address]"> <INPUT type=checkbox value=Yes name="userdata[3][checkbox]"> <INPUT size=42 name="userdata[3][name]"> <INPUT size=42 name="userdata[3][phone]"> <INPUT size=42 name="userdata[3][address]"> You would end up with a multidimenstional array in post: foreach ($_POST['userdata'] as $number => $data) { echo $data['name']; } Quote Link to comment Share on other sites More sharing options...
morraine Posted April 21, 2008 Author Share Posted April 21, 2008 Thank you both for you input! hitman6003 i think i will use you solution because i was thinking of a way to collect the data regardless of the amount of sections in the form because i may add more identical sections with javascript but your solution would compensate for any amount of section. Great! thanks guys 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.