473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5188
On May 16, 2:13 pm, lawp...@gmail.c om 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...@gma il.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...@gma il.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($_PO ST['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.comwro te:
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.comwro te:
>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.comwro te:
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14124
by: davidv | last post by:
I am building a very simple form that takes text and inserts data in a MySQL db. I would like my "logic" to simply insert the value in to the field in the database that matches the name from the query string. Here's the question- can I do this if I do not know how many name/value pairs are passed to the "logic" ? Can I just step through the $_POST array and use a do while $i <= count($_POST) or something?
6
10976
by: brian_mckracken | last post by:
This might not be the right group for this question, since its kind of a pure html question... Given the html construct: <form action='index.php?expand=0,10000' method='post'> Email: <input type='text' name='login' size='30'/> Password:<input type='password' name='login' size='30'/> </form>
5
5372
by: Chuck Anderson | last post by:
I have finally started coding with register_globals off (crowd roars - yeay!). This has created a situation that I am not sure how I should handle. I have scripts (pages) that can receive an input variable from the POST array (initial entry) or it could be in the GET array (go back and re-edit a form, for instance.) In my old sloppy scripting days this was no problem, as I had register_globals on and would merely access the the input...
3
1510
by: Fernando Rodríguez | last post by:
Hi, I'm writing code to validate fields in a form before saving to a db. All the validating functions are in a separate script which is required. All the validating functions add an error message to an array if the data doesn't validate. I check if something went wrong with count($theArray). Here's my code: ---------------------------------------------------------------------------
26
6270
by: drako | last post by:
Hi, I'm a bit stumped as I am getting a "Notice: Array to String Conversion" error when trying to do something that on the surface should be a very simple task - create an array, and write a set of values to them based on data submitted from POST Fields. Code below: $_SESSION = array();
3
3625
by: JackM | last post by:
Okay, I'm starting to get a little ticked off because I've worked for hours on this and I can't seem to find the cause. I'm using PHP 5.1.6. I'm trying to get the values of some form checkboxes along with another fixed variable and pass them along in an email. For demonstration purposes, I have checked the Wed and Fri checkboxes and set the time for 9:00 am. <FORM ACTION="test.php" METHOD=POST> <TABLE border="1" width="70%" > <TR>
2
15506
by: assgar | last post by:
I am having problems getting the user selected form info to inserted into the mysql database. I am also rec eving an error: Warning: Variable passed to each() is not an array or object in D:\web_server\webroot\common_list_process.php on line 1) I can display the contents of the $op array (see below) 2) Am I using the correct loop to unpact the array for inserting into the database?
4
4555
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first element so append a comma
3
4554
by: linghuyuanye | last post by:
I just learnt php yesterday. I have a problem whenever I submit the form on my webpage.It gives me Warning: Variable passed to each() is not an array or object. The array part in my form would be the group of checkbox. I set the name of the checkboxes to be "name=Contact_me" ,which should make it an array(or is it not?) I've searched for similar topics, trying to figure out what's wrong,but I just can't figure it out. Can somebody please help...
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.