acratus Posted October 12, 2009 Share Posted October 12, 2009 I'm having trouble with a three-dimensional $_POST array. It starts as a two-dimensional array on this side: <html> <head> <title>test</title> <script type="text/javascript"> var Dom = { get: function(el) { if (typeof el === 'string') { return document.getElementById(el); } else { return el; } }, add: function(el, dest) { var el = this.get(el); var dest = this.get(dest); dest.appendChild(el); }, remove: function(el) { // var el = this.get(el); // el.parentNode.removeChild(el); } }; var Event = { add: function() { if (window.addEventListener) { return function(el, type, fn) { Dom.get(el).addEventListener(type, fn, false); }; } else if (window.attachEvent) { return function(el, type, fn) { var f = function() { fn.call(Dom.get(el), window.event); }; Dom.get(el).attachEvent('on' + type, f); }; } }() }; Event.add(window, 'load', function() { var i = 2; Event.add('add-element', 'click', function() { var el = document.createElement('span'); el.innerHTML = '<input type="text" name="theArray['+i+'][\'name\']" value="">'; i++; Dom.add(el, 'content'); Event.add(el, 'click', function(e) { Dom.remove(this); }); }); }); </script> </head> <body> <p id="add-element">Add Elements</p> <form action="testPost.php" name="test" method="post"> <input type="text" name="theArray[1]['name']" value="foo" /> <input type="text" name="theArray[2]['name']" value="bar" /> <div id="content"></div> <input type="submit" name="submit" value="submit" /> </form> </body> </html> (If you're wondering why I only have one element it's because this is for a project where I will need nested lists later.) Now, onto testPost.php to process the code: <html> <head> <title>asdfdf</title> </head> <body> <ul> <?php for ($i=1;$i<=count($_POST["theArray"]);$i++){ echo("<li>".$_POST['theArray'][$i]['name']."</li>"); } ?> </ul> </body> </html> $_POST itself is an array, so we convert from a two-dimensional to a three-dimensional array. For some reason, only the first letter gets printed; the rest of the string gets truncated. My only request is that you don't suggest I just use a database or something of the like. This has to be easily portable and I will probably merge this into one document later. Thank you, Dan Quote Link to comment https://forums.phpfreaks.com/topic/177466-multidimensional-array-problems-in-_post/ Share on other sites More sharing options...
mikesta707 Posted October 12, 2009 Share Posted October 12, 2009 $_POST['theArray']['name'][$i] did you try that. I think multidimensional post arrays are set up like post - field - field[0] field[1] field[2] field2 field2[0] field2[1] etc Quote Link to comment https://forums.phpfreaks.com/topic/177466-multidimensional-array-problems-in-_post/#findComment-935717 Share on other sites More sharing options...
acratus Posted October 12, 2009 Author Share Posted October 12, 2009 $_POST['theArray']['name'][$i] did you try that. Okay, I tried it. Didn't work. The idea is I'm setting it up for the 'for' loop to where every 'name' is printed out in succession. I'll add code later for other variables, but I want to get it from the same $_POST array. Quote Link to comment https://forums.phpfreaks.com/topic/177466-multidimensional-array-problems-in-_post/#findComment-935723 Share on other sites More sharing options...
cbrooks Posted October 12, 2009 Share Posted October 12, 2009 in you testpost.php place the following to see exactly is being returned from the form: <?php print "<pre>"; print_r($_POST); print "<pre>"; ?> Just had a similar problem with multi dim arrays in $_POST and the above code showed me that all was not right. This code will display the array being returned in $_POST. In my problem it was how the value was being named in the form where you currently have: <input type="text" name="theArray[1]['name']" value="foo" /> <input type="text" name="theArray[2]['name']" value="bar" /> Quote Link to comment https://forums.phpfreaks.com/topic/177466-multidimensional-array-problems-in-_post/#findComment-935725 Share on other sites More sharing options...
acratus Posted October 12, 2009 Author Share Posted October 12, 2009 in you testpost.php place the following to see exactly is being returned from the form: <?php print "<pre>"; print_r($_POST); print "<pre>"; ?> Okay. I did what you said...the form adds elements dynamically, so I added two and this is what I got: Array ( [theArray] => Array ( [1] => Array ( ['name'] => foo ) [2] => Array ( ['name'] => bar ) [3] => Array ( ['name'] => sdfaasdf ) [4] => Array ( ['name'] => asdfasdf ) ) [submit] => submit ) This means the data is being passed correctly, right? Quote Link to comment https://forums.phpfreaks.com/topic/177466-multidimensional-array-problems-in-_post/#findComment-935728 Share on other sites More sharing options...
cbrooks Posted October 12, 2009 Share Posted October 12, 2009 try something like the following instead of you for loop: foreach ($_POST['thearray'] as $something=>$value){ echo("<li>".$value['name']."</li>"); } Quote Link to comment https://forums.phpfreaks.com/topic/177466-multidimensional-array-problems-in-_post/#findComment-935735 Share on other sites More sharing options...
acratus Posted October 13, 2009 Author Share Posted October 13, 2009 Okay, we're getting closer. This hunk of code seems to get me what I want, at least for now: foreach ($_POST["theArray"] as $key) { if(is_array($key)){ foreach ($key as $key2 => $value) { echo "<li>".$value."</li>"; } } else { echo ""; } } However, I still want to be able to call the varible out using ['name']. Quote Link to comment https://forums.phpfreaks.com/topic/177466-multidimensional-array-problems-in-_post/#findComment-935844 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.