View Full Version : $_POST for arrays or any other method. Help needed!!

07-25-2012, 11:53 AM
Hi all,

I am developing an application where forms are generated based on the administrator setup i.e. the admin would setup that question 1 is 'First name', it is a textbox, with max length of 20 and other validations and this question will appear on the user end as a form.

I am simply using the question numbers as names of the input items which makes it very easy to map to the DB.

I have created an option which generates more than 1 input item (From Date, To Date and Description). The admin would setup the answer type as 'range' and then input how many times should the 'range' answer type be repeated. This would print the From Date textbox, To date textbox and the Description textarea and repeat it as per the admin setup.

Now the problem:
- I am unsure of the naming convention that I should use. I tried f[question number][repeat index], but couldn't use $_POST on a two dimensional array. Could someone guide me as to how this is done. I have done this with single array for checkboxes, but this is a bit tricky.

- Is there a better method of naming these input items?

Thanks in advance.

07-25-2012, 04:16 PM
Naming is always up to you IMO... whatever makes your live easiest and easy to read. That being said, always make sure its one word, using underscore or camel case to distinguish words.

As for multidimensional arrays with post, they work fine. Can you show your attempt to it? It works the same as creating a single dimensional array in practice.

07-26-2012, 01:47 AM
AFAIK you can't transmit a multidimensional array via post or get. I may be wrong, I had many unsuccessful attempts when creating my generic form poster moons ago.

07-26-2012, 02:22 AM
You don't need to use a two dimensional array, per se, you just need to arrange in an 'n X m' manner with the proper text, to do that you use a 'modulus' % conditional statement when building the form, so that there is a line break, or the start of a new tr if you use a table. You can use the % statement at the $_POST end to recover the order of that data too. If you get the data from the inputs via javascript and post the javascript array via a hidden form element, it will appear in the $_POST statement as a comma separated string, you can use explode() to convert the data to an array, numbers may be seen as strings so you may need to specify the data type. Of course you have to avoid commas in the text in that case. What you want to do is entirely possible, it just takes a bit of coding and clear thinking about how to achieve it.

You could start with a simple form with text inputs, all the inputs that are given data would be used for the headings of a table FI , and the number of inputs with data would determine the width of the table. A special input with a separate name would determine the height of the table , or those numbers could be used to just make rows of textareas, not even in a table. Well, you should pretty much have the idea by now.

07-26-2012, 04:22 PM
Hm... I'm pretty sure I've done multidimensional arrays in the past... its possibly hit or miss, but its also possible I thought I did but came up with another technique for it. I'll review old code when I get home tonight.

07-26-2012, 04:41 PM
Hm... I'm pretty sure I've done multidimensional arrays in the past... its possibly hit or miss, but its also possible I thought I did but came up with another technique for it. I'll review old code when I get home tonight.I'm thinking that you can, but the only drawback is that you have to index the form elements when you build the form,
name="mydata[1][3]", whereas with a one dimensional array they can be name="mydata[]". However you may only have to index one layer, the top one. But I haven't tried it, I just may. Maybe you don't even have to index them, it depends on the way the code is written.

07-26-2012, 09:00 PM
I wrote this for something this morning, maybe you can adapt it

foreach ($_REQUEST['MenuID'] as $key => $value) {


and in your form have all the field names as juts

<input type="text" name="MenuID[]" >

07-26-2012, 09:10 PM
Don't use REQUEST! Always either POST or GET... know where you're getting your variables from!

07-26-2012, 10:37 PM
i use both get and post in the same script

07-26-2012, 10:50 PM
To set the same variable? You should always know where a variable is coming from.

If you set $userID via POST but use _REQUEST, and someone goes to the url with ?userID=1, you can't be sure of which userID you'll get. Its a security hole you can't afford and shouldn't. If you are setting the same variable from either POST or GET for some reason, set it explicitly:

$userID = intval(isset($_POST['userID'])?$_POST['userID']:$_GET['userID']));

This way you set the priority order, and you can include other variables to ensure the data is of the form you expect (add an extra hash or something to verify data).

Using REQUEST is not safe by any means.