Clearing up php retrieval of form variables

the basics on how to best submit and retrieve form values in php focusing on checkboxes because they seem to be the most diffucult to understand.

When posting form data to another page your naming convention makes all the difference in the world in your ease of access and clarity.

Your Options
simple syntax name="simple_syntax"
array syntax name="array_syntax[]"
multi-dimensional arrays name="multi-dimensional_array_syntax[][]"

simple syntax

<form name="av" method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
    <p>Simple checkbox <input type="checkbox" name="simple_checkbox_1" value="simple_checkbox_1"/>
    <input type="checkbox" name="simple_checkbox_2" value="simple_checkbox_2"/>
    <input type="checkbox" name="simple_checkbox_3" value="simple_checkbox_3"/></p>
</form>

assuming each checkbox was checked your $_POST should now look like this

$_POST => array (
    [simple_checkbox_1] => simple_checkbox_1
    [simple_checkbox_2] => simple_checkbox_2
    [simple_checkbox_3] => simple_checkbox_3
)

any checkbox unchecked would simply not show in the results: so if checkbox 2 was not checked you $_POST would be

$_POST => array (
    [simple_checkbox_1] => simple_checkbox_1
    [simple_checkbox_3] => simple_checkbox_3
)

note: this only applies to checkboxes: if we had used text inputs the result would be

$_POST => array (
    [text_1] => text_1
    [text_2] => 
    [text_3] => text_3
)

array syntax

<form name="av" method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
    <p>Simple array checkbox <input type="checkbox" name="simple_array_checkbox[]" value="simple_array_checkbox_1"/>
    <input type="checkbox" name="simple_array_checkbox[]" value="simple_array_checkbox_2"/>
    <input type="checkbox" name="simple_array_checkbox[]" value="simple_array_checkbox_3"/></p>
</form>

assuming each checkbox was checked your $_POST should now look like this

$_POST => array (
    [simple_array_checkbox] => Array
        (
            [0] => simple_array_checkbox_1
            [1] => simple_array_checkbox_2
            [2] => simple_array_checkbox_3
        )
)

leaving one unchecked will make that value dissapear from the array

$_POST => array (
    [simple_array_checkbox] => Array
        (
            [0] => simple_array_checkbox_1
            [1] => simple_array_checkbox_3
        )
)

notice that our array index does not skip a number for the unchecked checkbox

note: repeating form names without using array syntax will just overwrite any previous value

<form name="av" method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
    <input type="checkbox" name="array_checkbox" value="array_checkbox_1"/>
    <input type="checkbox" name="array_checkbox" value="array_checkbox_2"/></p>
</form>

will result in

$_POST => array (
    [array_checkbox] => array_checkbox_2
)

indexed array syntax

<form name="av" method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
    <p>Indexed array checkbox <input type="checkbox" name="indexed_array_checkbox[0]" value="indexed_array_checkbox_1"/>
    <input type="checkbox" name="indexed_array_checkbox[1]" value="indexed_array_checkbox_2"/>
    <input type="checkbox" name="indexed_array_checkbox[2]" value="indexed_array_checkbox_3"/></p>
</form>

assuming each checkbox was checked your $_POST should now look like this

$_POST => array (
    [indexed_array_checkbox] => Array
        (
            [0] => indexed_array_checkbox_1
            [1] => indexed_array_checkbox_2
            [2] => indexed_array_checkbox_3
        )
)

leaving one unchecked will make that value dissapear from the array

$_POST => array (
    [simple_array_checkbox] => Array
        (
            [0] => indexed_array_checkbox_1
            [2] => indexed_array_checkbox_3
        )
)

notice that our array index is maintained even with an unchecked checkbox

multi-dimensional array checkbox

<form name="av" method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
    <p>Multi-dimensional array checkbox <br/>
    Row 1 <input type="checkbox" name="complex_array_checkbox[row_1][]" value="complex_array_checkbox_row_1_1"/>
    <input type="checkbox" name="complex_array_checkbox[row_1][]" value="complex_array_checkbox_row_1_2"/>
    <input type="checkbox" name="complex_array_checkbox[row_1][]" value="complex_array_checkbox_row_1_3"/><br/>
    Row 2 <input type="checkbox" name="complex_array_checkbox[row_2][]" value="complex_array_checkbox_row_2_1"/>
    <input type="checkbox" name="complex_array_checkbox[row_2][]" value="complex_array_checkbox_row_2_2"/>
    <input type="checkbox" name="complex_array_checkbox[row_2][]" value="complex_array_checkbox_row_2_3"/><br/>
    Row 3 <input type="checkbox" name="complex_array_checkbox[row_3][]" value="complex_array_checkbox_row_3_1"/>
    <input type="checkbox" name="complex_array_checkbox[row_3][]" value="complex_array_checkbox_row_3_2"/>
    <input type="checkbox" name="complex_array_checkbox[row_3][]" value="complex_array_checkbox_row_3_3"/></p>
</form>

assuming each checkbox was checked your $_POST should now look like this

$_POST => array (
    [complex_array_checkbox] => Array
        (
            [row_1] => Array
                (
                    [0] => complex_array_checkbox_row_1_1
                    [1] => complex_array_checkbox_row_1_2
                    [2] => complex_array_checkbox_row_1_3
                )

            [row_2] => Array
                (
                    [0] => complex_array_checkbox_row_2_1
                    [1] => complex_array_checkbox_row_2_2
                    [2] => complex_array_checkbox_row_2_3
                )

            [row_3] => Array
                (
                    [0] => complex_array_checkbox_row_3_1
                    [1] => complex_array_checkbox_row_3_2
                    [2] => complex_array_checkbox_row_3_3
                )

        )
)

leaving some unchecked will make those values dissapear from the array

$_POST => array (
    [complex_array_checkbox] => Array
        (
            [row_1] => Array
                (
                    [0] => complex_array_checkbox_row_1_2
                    [1] => complex_array_checkbox_row_1_3
                )

            [row_2] => Array
                (
                    [0] => complex_array_checkbox_row_2_1
                    [1] => complex_array_checkbox_row_2_3
                )

        )
)

a fully unindexed multi-dimensional array checkbox is not very helpful using the same form your $_POST would be

$_POST => array (
    [complex_array_checkbox] => Array
        (
            [0] => Array
                (
                    [0] => complex_array_checkbox_row_1_1
                )

            [1] => Array
                (
                    [0] => complex_array_checkbox_row_1_2
                )

            [2] => Array
                (
                    [0] => complex_array_checkbox_row_1_3
                )

            [3] => Array
                (
                    [0] => complex_array_checkbox_row_2_1
                )

            [4] => Array
                (
                    [0] => complex_array_checkbox_row_2_2
                )

            [5] => Array
                (
                    [0] => complex_array_checkbox_row_2_3
                )

            [6] => Array
                (
                    [0] => complex_array_checkbox_row_3_1
                )

            [7] => Array
                (
                    [0] => complex_array_checkbox_row_3_2
                )

            [8] => Array
                (
                    [0] => complex_array_checkbox_row_3_3
                )

        )
)
commented: Nice Tutorial! +7
commented: Excellent summary of a confusing topic in HTML forms. Nice job! +4
Member Avatar
LastMitch

@jstfsklh211

Passing and reading form values in php

I'm bit confused is there any issue or error when you run the code?

hit the wrong post type its more of a tutorial
and it was too late to change it once i realized

Nice post, this is very clear and useful. Finding detailed examples on checkbox processing with PHP can often be elusive, so thanks for the post! Maybe one of the mods can update the post type.

Member Avatar
LastMitch

I get it now. It's very detail and simple.

very interesting one

Ha! I've been doing forms and php for years and had no idea you could just name your input elements with some array notation and it organize itself on the other end. Thanks!