TheNix Posted March 4, 2009 Share Posted March 4, 2009 if I send an array with post and I try to print it out on the next page it ends up just printing out "Array". I'd rather post all the elements of the array. For example. <form action="seats.php" method="post"> <?php foreach ($_POST as $value){ echo $value; echo "<input type=\"hidden\" name=\"pnames[]\" value=\"$value\">"; } ?> Enter Names: <input type="text" name="name" /><br/> Finished signing in? <input type="checkbox" name="ready" /><br/> <input type="submit" /> The form is posting to itself and adding the new entered name to the array of names already posted to be used later. What is happening is when it hits the foreach it will loop once for the hidden variable where the $value is "Array" no matter how many values are in the array, and once for the text box. Any help is appreciated, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/ Share on other sites More sharing options...
jackpf Posted March 4, 2009 Share Posted March 4, 2009 The way I do it is- I have several checkboxes: <input type="checkbox" name="checkbox[]" value="whatever" /> And the php process- foreach($_POST['checkbox'] as $key=>$value) { echo $value; echo '<input type="hidden" name="checkbox" value="'.$value.'" />'; } Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776406 Share on other sites More sharing options...
TheNix Posted March 4, 2009 Author Share Posted March 4, 2009 not working. Well first off the name of the hidden needs to have [] at the end of it to make it an array but I'm still getting a value of"Array" Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776418 Share on other sites More sharing options...
Stephen68 Posted March 4, 2009 Share Posted March 4, 2009 So you want to put the value of name field and ready field into the hidden value each time is it submitted? Wouldn't it be better to track this with session? Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776443 Share on other sites More sharing options...
TheNix Posted March 4, 2009 Author Share Posted March 4, 2009 well I'm trying to make a web based application that can take in names of poker players then assign them randomly to tables. My best idea was to keep a running array for every time someone enters a name and submits. Once they check the box that means they have all the names and I'd then have the form assign the names in the array randomly to a set amount of tables based on how many players there are. I'm trying to get the array to be the hidden value and add to it the text value, so it will be pname[] + name (ready is just to check if i should make the seatings instead) Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776458 Share on other sites More sharing options...
Stephen68 Posted March 4, 2009 Share Posted March 4, 2009 jackpf has it right I think just need the [] in the loop. You want to add the new name so you would have to add it in after of before the loop. <?php foreach($_POST['pnames'] as $key=>$value) { echo $value; echo '<input type="hidden" name="pnames[]" value="'.$value.'" />'; } //Now see if the form was submitted and if so add the new hidden name input field if (isset($_POST['submit'])) { //Was submitted so add new hidden input $name = $_POST['name']; echo '<input type="hidden" name="pnames[]" value="'.$name.'" />'; } ?> Didn't test this code but I think this might get you close to what you would like Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776460 Share on other sites More sharing options...
Stephen68 Posted March 4, 2009 Share Posted March 4, 2009 Sorry the I have been no help and wish I could remove my last post So you are going to have one person submit all the names of the players one at a time? Then once you hit a number you are going to create your tables with the players randomly picked to fill them? Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776469 Share on other sites More sharing options...
TheNix Posted March 4, 2009 Author Share Posted March 4, 2009 That is correct Stephen. I think I can do most of the work if i could get past the one problem. When I try to print the elements of an array passed through POST it is only printing "array" Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776480 Share on other sites More sharing options...
premiso Posted March 4, 2009 Share Posted March 4, 2009 <form action="seats.php" method="post"> <?php foreach ($_POST as $key => $value){ echo $value; echo "<input type=\"hidden\" name=\"pnames[$key]\" value=\"$value\">"; } ?> Enter Names: <input type="text" name="name" /><br/> Finished signing in? <input type="checkbox" name="ready" /><br/> <input type="submit" name="submit" /> See if that works how you want it. On a side note, is there a reason you are not using sessions to do this? <?php session_start(); $_SESSION['POST'] = $_POST; ?> <form action="seats.php" method="post"> Enter Names: <input type="text" name="name" /><br/> Finished signing in? <input type="checkbox" name="ready" /><br/> <input type="submit" name="submit" /> seats.php <?php session_start(); $postData = isset($_SESSION['POST'])?$_SESSION['POST']:array(); $_POST = array_merge($_POST, $postData); echo "<pre>" . print_r($_POST, 1) . "</pre>"; ?> Should give you a rough overview of how that would work. Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776490 Share on other sites More sharing options...
TheNix Posted March 4, 2009 Author Share Posted March 4, 2009 I didn't know I could use session but I'll try both and see what I can get to work, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776496 Share on other sites More sharing options...
premiso Posted March 4, 2009 Share Posted March 4, 2009 Well the session would not exactly work, as it would overwrite the previous data. At least in the sense I showed you. What I would do, given that only 1 name per page load is allowed is still use sessions, but do this instead. <?php session_start(); if (isset($_POST['name'])) { $_SESSION['names'][] = $_POST['name']; } ?> <form action="seats.php" method="post"> Enter Names: <input type="text" name="name" /><br/> Finished signing in? <input type="checkbox" name="ready" /><br/> <input type="submit" name="submit" /> Then to display them: <?php session_start(); $names = isset($_SESSION['names'])?$_SESSION['names']:array(); echo "<pre>" . print_r($names, 1) . "</pre>"; ?> Sorry for the first post, sometimes it makes sense in your head till you re-read it. Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776501 Share on other sites More sharing options...
TheNix Posted March 4, 2009 Author Share Posted March 4, 2009 Thank you this is leading me in the right direction and it works, now i just need to modify it to fit my use. Thank you again Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-776513 Share on other sites More sharing options...
jackpf Posted March 5, 2009 Share Posted March 5, 2009 Yeah, sorry guys, I forgot to put the [] at the end of the second checkbox. But that's exactly what I use to allow people to delete multiple emails on my site, and it works. If it doesn't work exactly, it's on the right lines at least. Just experiement Quote Link to comment https://forums.phpfreaks.com/topic/147932-sending-array-with-post/#findComment-777099 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.