470,862 Members | 1,731 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

a nested array in a $_POST variable?

Hello all -

Is there a way to get a nested array in a $_POST variable?

I have a form where there are several questions, each one
corresponding to a database row. On submission of the form, I loop
through the $_POST array, and update the database appropriately.

So my post variable looks like this:

$_POST =array(
'question_12' ='abc',
'question_14' ='xyz',
'question_27' ='123',
)

So I loop through the post array, and use substr() to get the question
id out of the key, to get something like "UPDATE questions SET
question = '" $value "' WHERE id = " substr( $key, 9 ); or something
like that.

Is there an easier way to do this? Ideally, I would like to get back a
$_POST array like this:

$_POST =array(
'0' =array(
'id' ='12',
'value' ='abc',
),
'1' =array(
'id' ='12',
'value' ='xyz',
),
'2' =array(
'id' ='27',
'value' ='123',
),
)

Am I dreaming? ;)

Jun 2 '08 #1
7 4921
On May 16, 2:13 pm, lawp...@gmail.com wrote:
Hello all -

Is there a way to get a nested array in a $_POST variable?

I have a form where there are several questions, each one
corresponding to a database row. On submission of the form, I loop
through the $_POST array, and update the database appropriately.

So my post variable looks like this:

$_POST =array(
'question_12' ='abc',
'question_14' ='xyz',
'question_27' ='123',
)

So I loop through the post array, and use substr() to get the question
id out of the key, to get something like "UPDATE questions SET
question = '" $value "' WHERE id = " substr( $key, 9 ); or something
like that.

Is there an easier way to do this? Ideally, I would like to get back a
$_POST array like this:

$_POST =array(
'0' =array(
'id' ='12',
'value' ='abc',
),
'1' =array(
'id' ='12',
'value' ='xyz',
),
'2' =array(
'id' ='27',
'value' ='123',
),
)

Am I dreaming? ;)
If your form element looks like this (I'm using a text field as an
example, but it could be any input type):

<input type="text" name="question[42]">

then you'll have $_POST['question'][42] when the form is submitted.
Then, it's just a matter of:

foreach($_POST['question'] as $id =$value) {
//do something
}
Jun 2 '08 #2
On May 16, 2:29 pm, ZeldorBlat <zeldorb...@gmail.comwrote:
If your form element looks like this (I'm using a text field as an
example, but it could be any input type):

<input type="text" name="question[42]">

then you'll have $_POST['question'][42] when the form is submitted.
Then, it's just a matter of:

foreach($_POST['question'] as $id =$value) {
//do something

}
Thanks for the reply.

Can you tell me what PHP version you're using, or some documentation
on this?

I tried it, and on my system, the value of the 'questions' element was
'Array'.

In other words,

$_POST =(
'questions' =Array,
...
)

So, it sort of half-worked for me.

Jun 2 '08 #3
On Fri, 16 May 2008 21:53:19 +0200, <la*****@gmail.comwrote:
On May 16, 2:29 pm, ZeldorBlat <zeldorb...@gmail.comwrote:
>If your form element looks like this (I'm using a text field as an
example, but it could be any input type):

<input type="text" name="question[42]">

then you'll have $_POST['question'][42] when the form is submitted.
Then, it's just a matter of:

foreach($_POST['question'] as $id =$value) {
//do something

}

Thanks for the reply.

Can you tell me what PHP version you're using, or some documentation
on this?

I tried it, and on my system, the value of the 'questions' element was
'Array'.
No, the value is an array, not the string 'Array'. And as you can't echo
an array, it get's substituted on output with that string. var_dump or
print_r the (values in your) POST array, don't print or echo them.
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #4
On May 16, 3:57 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Fri, 16 May 2008 21:53:19 +0200, <lawp...@gmail.comwrote:
I tried it, and on my system, the value of the 'questions' element was
'Array'.

No, the value is an array, not the string 'Array'. And as you can't echo
an array, it get's substituted on output with that string. var_dump or
print_r the (values in your) POST array, don't print or echo them.
No, the value of the 'questions' element in the POST array really is
the string 'Array'.

I'm using a select tag like this:
--------------
"<select name='question[11533]'>"

After submission, I'm using the code:
------------------------
echo "<pre>" . print_r( $_POST, 1 ) . "</pre>";

And I get this:
---------------
Array
(
[card_id] =535
[question] =Array
[survey] =Update
)

Is it because I'm trying to do it with a select tag?
Jun 2 '08 #5
I've also tested this with hidden and text inputs:
-----------------------------------------
<input type='hidden' name='test[1]' value='a'>
<input type='hidden' name='test[2]' value='b'>
<input type='hidden' name='test[3]' value='c'>
<input type='test' name='test2[1]' value='a'>
<input type='test' name='test2[2]' value='b'>
<input type='test' name='test2[3]' value='c'>

And using this print_r code:
------------------------
echo "<pre>" . print_r( $_POST, 1 ) . "</pre>";

gives this result:
--------------------
Array
(
[client_id] =540
[question] =Array
[test] =Array
[test2] =Array
[form] =Update
)

Then, to make sure that my print_r code was good, and not mangling
actual arrays into the string 'Array', I did this:
---------------------------------
$_POST['real_array'] = array(
'1' ='a',
'2' =array(
2,
3,
4,
),
'5' ='cat',
);

And then used the same code to regurgitate the POST with the nested
array:
---------------------------------------
echo "<pre>" . print_r( $_POST, 1 ) . "</pre>";

And got this:
------------------
Array
(
[client_id] =540
[question] =Array
[test] =Array
[test2] =Array
[form] =Update
[real_array] =Array
(
[1] =a
[2] =Array
(
[0] =2
[1] =3
[2] =4
)

[5] =cat
)
)
Thoughts...?
Jun 2 '08 #6
On Fri, 16 May 2008 22:02:33 +0200, <la*****@gmail.comwrote:
On May 16, 3:57 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
>On Fri, 16 May 2008 21:53:19 +0200, <lawp...@gmail.comwrote:
I tried it, and on my system, the value of the 'questions' element was
'Array'.

No, the value is an array, not the string 'Array'. And as you can't echo
an array, it get's substituted on output with that string. var_dump or
print_r the (values in your) POST array, don't print or echo them.

No, the value of the 'questions' element in the POST array really is
the string 'Array'.

I'm using a select tag like this:
--------------
"<select name='question[11533]'>"

After submission, I'm using the code:
------------------------
echo "<pre>" . print_r( $_POST, 1 ) . "</pre>";

And I get this:
---------------
Array
(
[card_id] =535
[question] =Array
[survey] =Update
)

Is it because I'm trying to do it with a select tag?
No.
<html>
<head><title></title></head>
<body>
<form action="<?php echo $_SERVER['REQUEST_URI'];?>" method="post">
<select name="foo[123]">
<option>bar</option>
<option>baz</option>
</select>
<input type="submit">
</form>
<pre><?php print_r($_POST); ?></pre>
</body>
</html>

Output:
Array
(
[foo] =Array
(
[123] =bar
)

)

If that is the actual contents of your $_POST array (double check in your
HTML source), this would mean the POST array is abused earlier on in your
code. Try it: just post to a page consisting of nothing but <?php
var_dump($_POST); ?>, and it will be there. If my suspisions are correct,
I have another example in my arsenal why people should treat the $_POST
array as read only and only operate on / alter (partial) copies of the
data if needed.
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #7
On May 16, 4:20 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
If that is the actual contents of your $_POST array (double check in your
HTML source), this would mean the POST array is abused earlier on in your
code.
:)

That's exactly what's going on. Thank you!

A crufty old include from a previous incarnation of the project. Now
it works perfectly!
Jun 2 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by davidv | last post: by
6 posts views Thread by brian_mckracken | last post: by
5 posts views Thread by Chuck Anderson | last post: by
3 posts views Thread by Fernando Rodríguez | last post: by
4 posts views Thread by mab464 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.