Can I put associative arrays in form inputs to be processed in PHP?
Asked Answered
S

4

22

I know I can do things like <input name="foo[]">, but is it possible to do things like <input name="foo[bar]"> and have it show up in PHP as $_POST['foo']['bar']?

The reason I ask is because I'm making a huge table of form elements (including <select> with multiple selections), and I want to have my data organized cleanly for the script that I'm POSTing to. I want the input elements in each column to have the same base name, but a different row identifier as an array key. Does that make sense?

EDIT: I tried exactly this already, but apparently Drupal is interfering with what I'm trying to do. I thought I was just getting my syntax wrong. Firebug tells me that my input names are constructed exactly like this, but my data comes back as [foo[bar]] => data rather than [foo] => array([bar] => data).

EDIT 2: It seems my real problem was my assumption that $form_state['values'] in Drupal would have the same array hierarchy as $_POST. I should never have assumed that Drupal would be that reasonable and intuitive. I apologize for wasting your time. You may go about your business.

Sorbitol answered 24/6, 2011 at 14:15 Comment(3)
That's what the manual says php.net/manual/en/faq.html.php#faq.html.arraysKingfish
possible duplicate of Posting array from formKingfish
Odd, I tried to do exactly this and it didn't work. I guess it's just because I'm using Drupal :/ I thought I just wasn't doing the PHP syntax right. Now I'm not sure if I should revise this question or write a new, Drupal-specific question.Sorbitol
E
5

You can do this in Drupal too, quite easily. The important thing you have to remember about is setting form '#tree' parameter to TRUE. To give you a quick example:

function MYMODULE_form() {
  $form = array('#tree' => TRUE);
  $form['group_1']['field_1'] = array(
    '#type' => 'textfield',
    '#title' => 'Field 1',
  );
  $form['group_1']['field_2'] = array(
    '#type' => 'textfield',
    '#title' => 'Field 2',
  );
  $form['group_2']['field_3'] = array(
    '#type' => 'textfield',
    '#title' => 'Field 3',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

Now, if you print_r() $form_state['values'] in MYMODULE_form_submit($form, &$form_state), you will see something like this:

Array
(
    [group_1] => Array
        (
            [field_1] => abcd
            [field_2] => efgh
        )

    [group_2] => Array
        (
            [field_3] => ijkl
        )

    [op] => Submit
    [submit] => Submit
    [form_build_id] => form-7a870f2ffdd231d9f76f033f4863648d
    [form_id] => test_form
)
Eckblad answered 24/6, 2011 at 14:45 Comment(1)
Oooohh I didn't know about #tree. I have my form arrays structured similar to this, but I didn't know there was an extra flag in there I was missing. Thanks!Sorbitol
A
32

Let say we want to print student scores using the form below:

<form action="" method="POST">
  <input name="student['john']">
  <input name="student['kofi']">
  <input name="student['kwame']">
  <input type="submit" name="submit">
</form>

and PHP code to print their scores:

if(isset($_POST['submit']))
{
    echo $_POST['student']['john'] . '<br />';
    echo $_POST['student']['kofi'] . '<br />';
    echo $_POST['student']['kwame'] . '<br />'; 
}

This will print the values you input into the field.

Ascomycete answered 24/6, 2011 at 14:59 Comment(1)
I tried your solution but I'm wondering why I have to use it like this: $_POST['student']['\'john\''] ? I also use it in the form like this: <input name="student[kofi]">Farr
H
8

Yes you can. you can even do name="foor[bar][]" and on for even more padding.

Hereditary answered 24/6, 2011 at 14:18 Comment(0)
E
5

You can do this in Drupal too, quite easily. The important thing you have to remember about is setting form '#tree' parameter to TRUE. To give you a quick example:

function MYMODULE_form() {
  $form = array('#tree' => TRUE);
  $form['group_1']['field_1'] = array(
    '#type' => 'textfield',
    '#title' => 'Field 1',
  );
  $form['group_1']['field_2'] = array(
    '#type' => 'textfield',
    '#title' => 'Field 2',
  );
  $form['group_2']['field_3'] = array(
    '#type' => 'textfield',
    '#title' => 'Field 3',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

Now, if you print_r() $form_state['values'] in MYMODULE_form_submit($form, &$form_state), you will see something like this:

Array
(
    [group_1] => Array
        (
            [field_1] => abcd
            [field_2] => efgh
        )

    [group_2] => Array
        (
            [field_3] => ijkl
        )

    [op] => Submit
    [submit] => Submit
    [form_build_id] => form-7a870f2ffdd231d9f76f033f4863648d
    [form_id] => test_form
)
Eckblad answered 24/6, 2011 at 14:45 Comment(1)
Oooohh I didn't know about #tree. I have my form arrays structured similar to this, but I didn't know there was an extra flag in there I was missing. Thanks!Sorbitol
C
3

Too late to answer but its important to highlight an aspect of @Ernest Boabramah answer.

If you send your input data as array having keys inside quotes as follows

<input name="student['john']">
<input name="student['kofi']">

Then you must escape those quotes around keys while accessing the value in php

echo $_POST['student']['\'john\''];
echo $_POST['student']['\'kofi\'']

I would suggest you to remove those quotes around the keys

<input name="student[john]">
<input name="student[kofi]">

so you can access them without escaping anything

echo $_POST['student']['john'];
echo $_POST['student']['kofi']; 
Chamonix answered 18/1, 2022 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.