limitphp Posted April 9, 2009 Share Posted April 9, 2009 I have a form submitting an unknown number of textboxes named: fname1, fname2, fname3, etc.... How do I capture the values? I figure using an array would be easiest.... $i=1; $fname = array(); while ($i<=$num) { $fname[$i] = $_POST['fname'.$i]; $i++; } $i=1; while ($i<=$num) { echo $fname[$i]." ".$lname[$i]." ".$position[$i]."<br><br>"; $i++; } This doesn't seem to be working... its not showing anything.... Link to comment https://forums.phpfreaks.com/topic/153376-help-with-array/ Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Why don't you name all of your textbox the same thing "fname[]". Then in your processing script you can just do: foreach($_POST['fname'] AS $fname) echo $fname; Link to comment https://forums.phpfreaks.com/topic/153376-help-with-array/#findComment-805794 Share on other sites More sharing options...
eiledon01 Posted April 9, 2009 Share Posted April 9, 2009 try something like this: $i=1; $fnames = array(); foreach($_POST as $fld=>$val){ if(eregi("fname",$fld)){ $fnames[$i] = $val; $i++ } } $i = 1; foreach($fnames as $fname){ echo $fname[$i]." ".$lname[$i]." ".$position[$i]."<br><br>"; $i++; } Link to comment https://forums.phpfreaks.com/topic/153376-help-with-array/#findComment-805796 Share on other sites More sharing options...
limitphp Posted April 9, 2009 Author Share Posted April 9, 2009 actually, I'll need to do more stuff than just echo them...I'll need to add the values to a database...and there will be more than just fname...there will be lname and position as well. So, if I change the textboxes to fname[], lname[], and position[] also, I'll know the exact number....from $num.... how would I grab each value? ex) loop (as many times as $num) { do stuff to each value of fname, lname and position } thanks... Link to comment https://forums.phpfreaks.com/topic/153376-help-with-array/#findComment-805808 Share on other sites More sharing options...
limitphp Posted April 9, 2009 Author Share Posted April 9, 2009 I read this on php.net: Many web sites do this: <form ....> <input name="person_0_first_name" value="john" /> <input name="person_0_last_name" value="smith" /> ... <input name="person_1_first_name" value="jane" /> <input name="person_1_last_name" value="jones" /> </form> //Which is exactly what i was trying to do, because I do not know any BETTER.... When they could do this: <form ....> <input name="person[0][first_name]" value="john" /> <input name="person[0][last_name]" value="smith" /> ... <input name="person[1][first_name]" value="jane" /> <input name="person[1][last_name]" value="jones" /> </form> With the first example you'd have to do string parsing / regexes to get the correct values out so they can be married with other data in your app... whereas with the second example.. you will end up with something like: <?php var_dump($_POST['person']); //will get you something like: array ( 0 => array('first_name'=>'john','last_name'=>'smith'), 1 => array('first_name'=>'jane','last_name'=>'jones'), ) ?> I sort of understand what they are saying, but not quite.... How would I apply that to what I'm doing? Can someone help me? I didn't know arrays could get that complicated.... Link to comment https://forums.phpfreaks.com/topic/153376-help-with-array/#findComment-805822 Share on other sites More sharing options...
limitphp Posted April 9, 2009 Author Share Posted April 9, 2009 Why don't you name all of your textbox the same thing "fname[]". Then in your processing script you can just do: foreach($_POST['fname'] AS $fname) echo $fname; Would I need to do a foreach loop for each fname, lname, and position? Or is there a way to combine all of them in one loop? Link to comment https://forums.phpfreaks.com/topic/153376-help-with-array/#findComment-805833 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.