boff2000 Posted September 12, 2006 Share Posted September 12, 2006 How can I send an array from a form? I try creating a hidden input element with name='array_name' and value='$array_name' , but it doesnt seem to happen for me. I know I'm doing something wrong :'(Im quite sure this is totally a newbie question :-[ Quote Link to comment https://forums.phpfreaks.com/topic/20516-sending-arrays-with-a-html-form-newbie-question/ Share on other sites More sharing options...
ober Posted September 12, 2006 Share Posted September 12, 2006 name='array_name[]' Quote Link to comment https://forums.phpfreaks.com/topic/20516-sending-arrays-with-a-html-form-newbie-question/#findComment-90467 Share on other sites More sharing options...
boff2000 Posted September 12, 2006 Author Share Posted September 12, 2006 hm, ok i tried that to no avail. i think id better paste an example:[code]<?phpif(!$cmd){ $array_name = array( "Some text" ); echo $array_name[0];} echo "<form action='page.php' method='post'> <input type='hidden' name='array_name[]' /> <input type='submit' name='cmd' value='go' /> </form>";switch($_REQUEST['cmd']){ case "go": echo $array_name[0]; break; }?>[/code] ??? Quote Link to comment https://forums.phpfreaks.com/topic/20516-sending-arrays-with-a-html-form-newbie-question/#findComment-90477 Share on other sites More sharing options...
Jenk Posted September 12, 2006 Share Posted September 12, 2006 [code]<?phpforeach ($array as $value){ echo '<input type="hidden" name="array_name[]" value="' . htmlentities($value) . '" />' . chr(10);}?>[/code][/code] Quote Link to comment https://forums.phpfreaks.com/topic/20516-sending-arrays-with-a-html-form-newbie-question/#findComment-90481 Share on other sites More sharing options...
.josh Posted September 12, 2006 Share Posted September 12, 2006 according to your logic, $array_name will always be set to "some text" whether you click submit or not, because $cmd is never created. if you have not submitted your form, "some text" will be echoed once: inside your if statement. if you do submit your form, it will be echoed twice: once inside your if statement, and then again inside your switch statement. Both times it will still say "some text". Your hidden input named array_name is passed with no value, but it is always overwritten, due to your if statement. i can plainly see what your example code does. however, i'm not entirely sure what it is [i]you[/i] are wanting it to do. so here is an example:[code]<?php if ($_POST['items']) { foreach ($_POST['items'] as $val) { echo "$val <br>"; } }?><form action = '<?php echo $_SERVER['PHP_SELF']; ?>' method = 'post'> <input type = 'hidden' name = 'items[]' value = 'value1'> <input type = 'hidden' name = 'items[]' value = 'value2'> <input type = 'hidden' name = 'items[]' value = 'value3'> <input type = 'submit' name = 'submit' value = 'submit'></form>[/code]what this does is creates a form with a submit button. the 3 hidden input types makes an array called 'items' with 3 values. when you click submit, it checks to see if items exists, and if so, then loop through and echo all the values. Quote Link to comment https://forums.phpfreaks.com/topic/20516-sending-arrays-with-a-html-form-newbie-question/#findComment-90488 Share on other sites More sharing options...
boff2000 Posted September 12, 2006 Author Share Posted September 12, 2006 Thanks for the replies! Crayon Violent - yes i understand whats happening - i echoed the the results twice to test if each time returned the same result.Anyway - for the example i provided, the solutions you guys provided worked for it..however, i thought i may have been able to apply that logic to what im doing. i actually want to send a multi-dimensional array. The reason being, I want to send it somewhere so that other functions can then edit elements of the array - then ultimately have it all write to XML (but ive got that bit sorted :P )im doing something wrong about half way through this code.. i bet[code]<?php//Create the array only when $cmd does not existif(!$cmd){$arr = array();$arr[0][0] = "a";$arr[0][1] = "b";$arr[1][0] = "y";$arr[1][1] = "z"; }echo "<form action='page.php' method='post'>";//put the multi-dimensional array into input elementsforeach ($arr as $v1) { foreach ($v1 as $v2) { echo "<input type='hidden' name='arr[]' value='$v2' />"; }}echo "<input type='submit' name='cmd' value='go' /></form>";//If $cmd exists, return the arrayswitch($_REQUEST['cmd']){ case "go": foreach ($arr as $v1) { foreach ($v1 as $v2) { echo $v2; } } break; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20516-sending-arrays-with-a-html-form-newbie-question/#findComment-90557 Share on other sites More sharing options...
Jenk Posted September 13, 2006 Share Posted September 13, 2006 [code]foreach ($arr as $k1 => $v1) { foreach ($v1 as $k2 => $v2) { echo "<input type='hidden' name='arr[$k1][$k2]' value='$v2' />"; }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20516-sending-arrays-with-a-html-form-newbie-question/#findComment-90829 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.