Posting a multidimensional array
A brief tutorial (my first in fact) covering a simple method for posting a multidimensional array using an html form.
Posting arrays is a fact of life - by default every time you post anything from an html form it's posted as an array. It's your job to break it all down again when you process the form. After a while this becomes second nature and you forget that you're even dealing with an array in the first place.
However, sometimes you want to keep a posted value as an array. The usual reason for this is storing a list of options. For instance you might want to give a member of your site the opportunity to set their own screen colours but, for whatever reason, you just want to store those options in one field of your database (or a single cookie, even).
The html code for such a form might be:
<form method="post" action="post-script.php" id="colours_form">
<p><strong>Text colour:</strong>
<input type="radio" name="colour[text]" value="red" />Red
<input type="radio" name="colour[text]" value="blue" />Blue
<input type="radio" name="colour[text]" value="green" />Green
<input type="radio" name="colour[text]" value="yellow" />Yellow
</p>
<p><strong>Background colour:</strong>
<input type="radio" name="colour[background]" value="red" />Red
<input type="radio" name="colour[background]" value="blue" />Blue
<input type="radio" name="colour[background]" value="green" />Green
<input type="radio" name="colour[background]" value="yellow" />Yellow
</p>
<p>
<input type="submit" name="submit" value="Go" />
</p>
</form>
In PHP you would access the posted colours array simply by using $_POST['colour'], and then use serialize to get it into a database friendly format. You can see the form in action here: http://www.evilchicken.biz/tutorials/post_array.php
However, what we really want to discuss here is posting multidimensional arrays, so we'll press on. In truth there aren't likely to be many occasions where you'll need to post multidimensional arrays. In my case I needed to give a client the ability to set a series of custom links (using text and url fields) without forcing them to code the links directly in html. I'll use pretty much the same example here.
Adapting the above form code for the titles/url example might look something like this:
<form method="post" action="post-script.php" id="url_form">
<p><strong>Link Text:</strong>
<input type="text" name="link[text]" />
</p>
<p><strong>Link Url:</strong>
<input type="text" name="link[url]" />
</p>
<p>
<input type="submit" name="submit" value="Go" />
</p>
</form>
That's all very well, but what if we need to post several links? Simple - all we do is add a numerical key to our form:
<form method="post" action="post-script.php" id="url_form">
<p><strong>Link Text:</strong>
<input type="text" name="link[0][text]" />
<input type="text" name="link[1][text]" />
<input type="text" name="link[2][text]" />
<input type="text" name="link[3][text]" />
</p>
<p><strong>Link Url:</strong>
<input type="text" name="link[0][url]" />
<input type="text" name="link[1][url]" />
<input type="text" name="link[2][url]" />
<input type="text" name="link[3][url]" />
</p>
<p>
<input type="submit" name="submit" value="Go" />
</p>
</form>
It took me a while to get this right. At first I missed out the numerical key entirely (e.g. <input type="text" name="link[text][]" /> ) which just resulted in each entry being overwritten by the next one. Then I had the key on the wrong side. Still I got there in the end.
You can play with a demo here: http://www.evilchicken.biz/tutorials/post_multi_array.php
Andrei June 10, 2009 - 15:06
Thanks for the tutorial, but where is the php code?