PDA

View Full Version : Passing arrays with HTTP Forms


HitByASquirrel
01-02-03, 23:44
... is this possible ... and how would it be done?
thanks ^_^

bcyde
01-03-03, 13:32
You may want to provide a little more info so that a better example can be tailored to your situation, but let's say you have an array on a page that needs to be submitted to the next one and you are using a hidden form field to do this. You can just use the implode function (http://www.php.net/manual/en/function.implode.php) to convert the array to a character delimted string and pass that in the hidden form field and on the next page just use explode (http://www.php.net/manual/en/function.explode.php) to extract the contents back into an array.

Also, you may opt to just use a session variable instead of a form field depending on what you are trying to do.

HTH
-b

aZa
01-17-03, 18:06
Hmmm ... Some time ago I've encountered with the following problem: I was needed to generate a page that collects data from the user in variable number of input boxes and then passes all that data to the next page for processing ...

I could easily do naming of the input boxes on the first page. So ideally it looked like creating an array of input_boxes[] and giving all input boxes on the first page names like input_box[1], input_box[2], input_box[3] and so on. And then easily process all data using this array. But, HTML forms seems to be forbidding passing array data ... Or am I wrong?

P.S.: I've solved the problem using a loop that automatically creates different names for every input_box on the page: input1, input2, input3 .... But of course if I could use array variable instead that would be much more comfortable with further coding ...

bcyde
01-17-03, 18:18
If you're talking about arrays for form text input fields you may want to look at variable variables http://www.devshed.com/Server_Side/PHP/VarVar/page1.html .

aZa
01-17-03, 19:40
Yep, variable variables is what I used in my solution ;). But I still dreamed if it could be possible to use something like:

<form action="processing.php">
<input type="text" name="array[1]">
<input type="text" name="array[2]">
..........
<input type="submit">
</form>

And then accessing data from these field in processing.php like $array[1], $array[2] ... But it's not possible and the only solution is using variable variables for processing all input boxes (naming them input1, input2 ...), right? :rolleyes:

aZa
01-17-03, 19:46
Originally posted by bcyde
...
http://www.devshed.com/Server_Side/PHP/VarVar/page1.html

Paragraph about "Never-ending variables" looks astonishing! :D :D
Thanks for link, nice article!