0

I have the following checkbox created by a loop:

<input type='checkbox' value=0 name='<?php echo $question_id; ?>' />

Each checkbox will have a different ID, and when the user checks several checkboxes, I would like to gather all the id's and insert it into a table that relates an 'event' table with a 'questions' table.

However I am not getting the ids...I know I am supposed to use an array, but is that in the name attribute or the values attribute? Also, how do I pass on the IDs...does that go into the values field?

1
  • You can use the implode function implode Commented Jul 19, 2011 at 17:46

4 Answers 4

3

In HTML:

<input type='checkbox' value=1 name='checkboxname[<?php echo $question_id; ?>]' />

In PHP:

$set_checkboxes = array_keys($_POST['checkboxname']);

Or, alternatively:

In HTML:

<input type='checkbox' value="<?php echo $question_id; ?>" name='checkboxname[]' />

In PHP:

$set_checkboxes = $_POST['checkboxname'];
Sign up to request clarification or add additional context in comments.

Comments

1

I believe you need something like ..

<input type='checkbox' value=<SOME UNIQUE VALUE> name='field_name[]' />

You'll loop through the field_name which happens to be an array.

2 Comments

That's great...does the value='have to be in quotes' or only if it's a string? I am passing a number/id...
I use quotes (sorry i missed it).
0

Try changing your input to this.

<input type='checkbox' value='<?php echo $question_id; ?>' name='questions[]' />

Now when you get the post with php it will return an array with all the question ids

$questionIds = $_POST['questions'];

Comments

0

Your html:

<form method="post" action="">
<input type="checkbox" id="things[]" value="red"> Cat<br>
<input type="checkbox" id="things[]" value="blue"> Mouse<br>
<input type="checkbox" id="things[]" value="green"> Car<br>
<input type="checkbox" id="things[]" value="yellow"> DaniWeb
</form>

Your PHP:

<?php
$things = serialize($_POST['things']);

$query = "INSERT INTO table(things) values($things)";
mysql_query($query) or die(mysql_error());
?>

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.