28

I am trying to send data from multiple checkboxes (id[]) and create an array "info" in php to allow me to run a script for each value (however the quantity of values may change each time) however first I am trying to display the content of each array value. I am not quite sure how to put my array populating line to save all the content to the array.

HTML

echo("<input name='id[]' type='checkbox' value='".$shopnumb."'>");

my hopeful processing code currently is -

$info=$_POST['id[]'];
Echo(array_values($info));

what do I need to do to make the content sent by post from the form checkboxes populate the array info

any help is greatly appreciated

edited for clarification.

4 Answers 4

44

Change

$info=$_POST['id[]'];

to

$info=$_POST['id'];

by adding [] to the end of your form field names, PHP will automatically convert these variables into arrays.

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

1 Comment

thank you this was probably my biggest problem - I wasn't even thinking about it.
17

You should get the array like in $_POST['id']. So you should be able to do this:

foreach ($_POST['id'] as $key => $value) {
    echo $value . "<br />";
}

Input names should be same:

<input name='id[]' type='checkbox' value='1'>
<input name='id[]' type='checkbox' value='2'>
...

1 Comment

This is the only answer which really makes sense
8

On the form page, field names must look like this

<input name="id[]" type="checkbox" value="x">
<input name="id[]" type="checkbox" value="y">
<input name="id[]" type="checkbox" value="z">

On the destination page, $_POST['id'] is your array variable

$id = implode(",", $_POST['id']);
echo $id; //Should print "1,2,3"

You cannot echo an array directly, because it will just print out "Array". If you wanna print out the array values use print_r.

print_r($_POST['id']);

4 Comments

You used id[] which means that you are passing an array. This is used if you want to pass multiple checkboxes. If you just want to pass one checkbox just use name="id"
I am passing possibly 1 possibly 500
And how to performe an update with such posted arrays? foreach ($_POST[id] as $id) { $update = UPDATE table SET name = ??? WHERE id = $id}? name is also an array
OK I got it - I count elements in array and repeat update as many times as counted times with other array variables looked like $_POST[var][$i] - and that is it
2

I don't know if I understand your question, but maybe:

foreach ($_POST as $id=>$value)
    if (strncmp($id,'id[',3) $info[rtrim(ltrim($id,'id['),']')]=$_POST[$id];

would help

That is if you really want to have a different name (id[key]) on each checkbox of the html form (not very efficient). If not you can just name them all the same, i.e. 'id' and iterate on the (selected) values of the array, like: foreach ($_POST['id'] as $key=>$value)...

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.