robcrozier Posted November 3, 2006 Share Posted November 3, 2006 ok, im trying to retrieve some variables which were created using a form. Each variable was initialized like so:[code]<input name="page_name<? $i ?>" type="text" class="style183" size="40">[/code]then if the form is submitted i'm trying to access the variables like this:[code]for ($i = 1; $i <= (($_SESSION['no_of_pages'])+2); $i++){ $page_name[$i] = $_POST['page_name[$i]']; echo "".$page_name[$i];}[/code]Its the $_POST['page_name[$i]']; bit that i think may be wrong. Can anyone shed any light on this?Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/26047-_post-array-variables/ Share on other sites More sharing options...
AbydosGater Posted November 3, 2006 Share Posted November 3, 2006 Hey, Im not too sure myself..But should it not be $page_name[$i] = $_POST['page_name']['i'];All depends on what you want it to do Quote Link to comment https://forums.phpfreaks.com/topic/26047-_post-array-variables/#findComment-119069 Share on other sites More sharing options...
robcrozier Posted November 3, 2006 Author Share Posted November 3, 2006 Cheers mate!Well some progress...It's now printing only the last variable from the array the ammount of times the loop states. Why do you think it's only picking up the last variable??? Quote Link to comment https://forums.phpfreaks.com/topic/26047-_post-array-variables/#findComment-119070 Share on other sites More sharing options...
mausie Posted November 3, 2006 Share Posted November 3, 2006 Not sure I really get it butI think it should be:[code]for ($i = 0; $i <= (($_SESSION['no_of_pages'])+2); $i++){ $page_name = $_POST['page_name[$i]']; echo "".$page_name;}[/code]why use $i on page_name?Arrays are zero based. Start at 0.Hope this helps you. If not, sorry that I might of misunderstood your code. Quote Link to comment https://forums.phpfreaks.com/topic/26047-_post-array-variables/#findComment-119104 Share on other sites More sharing options...
king arthur Posted November 3, 2006 Share Posted November 3, 2006 Try[code] $page_name[$i] = $_POST['page_name']['$i'];[/code]? Quote Link to comment https://forums.phpfreaks.com/topic/26047-_post-array-variables/#findComment-119107 Share on other sites More sharing options...
kenrbnsn Posted November 3, 2006 Share Posted November 3, 2006 Going back to the OP's original code. If the input line is[code]<input name="page_name<? $i ?>" type="text" class="style183" size="40">[/code]as was stated, then none of the solutions will work since the above statement will not really work as intended. The correct line would be either[code]<input name="page_name<?php echo $i ?>" type="text" class="style183" size="40">[/code]or[code]<input name="page_name[<?php echo $i ?>]" type="text" class="style183" size="40">[/code]I personally prefer the second form, since then the incoming values can be referenced as values in the $_POST['page_name'] array:[code]<?phpfor ($i=0;$i<count($_POST['page_name']);$i++) {//// do your code//}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/26047-_post-array-variables/#findComment-119121 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.