2
<input type="checkbox" id="one" name="form[age[]]" value="1"/><label for="one">0 to 12 months</label> 

How do I reference the name in PHP once it has been submitted using POST. I am trying to collect all the checkbox values and then implode age[], but I need to put this in a form[] for form validation.

When i print_r[$_POST['form']['age'], it displays Notice: Undefined index: age

3 Answers 3

1

Simply use the key form and the key age in form:

$age = $_POST['form']['age'];
$imploded = implode(',', $age);

The value of the HTML-Attribute name is the key in $_POST, so form is $_POST['form']

Edit: The syntax of your name value is wrong, use form[age][] instead.

Sign up to request clarification or add additional context in comments.

1 Comment

its not working. in that age[] only displays the last checked box. am i input the name= wrong?
0

You should use name="form[age][]" for every checkbox with age value, and then after you send those values to PHP they will be available as an array $_POST['form']['age'].

Example:

<input type="checkbox" name="form[age][]" value="1"/>
<input type="checkbox" name="form[age][]" value="2"/>
<input type="checkbox" name="form[age][]" value="3"/>

will result an array in PHP like:

$_POST['form']['age'][0]; // = 1
$_POST['form']['age'][1]; // = 2
$_POST['form']['age'][2]; // = 3

Comments

0

I think you want this:

<input type="checkbox" id="one" name="form[age][]" value="1"/>
<input type="checkbox" id="one" name="form[age][]" value="2"/>
.
.
<input type="checkbox" id="one" name="form[age][]" value="12"/>

<?
$implode = implode(',', $_POST['form']['age']);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.