-2

I am creating a set of checkboxes dynamically using PHP based on the value from databases.My requirement is to get the ID of the checked checkboxes and pass it to the next page when user clicks the submit button via POST.I am not using AJAX.

For example I need something like this

first page:

<input type="checkbox" name="check[]" id="checkme<?php echo var; ?>">.......

secondpage.php:

$getcheckboxID = $_POST['check[]']
Techy
  • 2,626
  • 7
  • 41
  • 88
  • this should help you http://stackoverflow.com/questions/10145717/deleting-multiple-rows-using-checkboxes-php-and-mysql – user3587554 May 12 '14 at 07:35

2 Answers2

6

Form controls submit their name and value to the server, not their id (that's for client side use only). If, as in your example, they have no value then they cannot be successful and won't submit anything.

Store the data you are after in the value.

PHP also parses a name like check[] into an array called check, so you need to loop over $_POST['check'] and not try to access $_POST['check[]']

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Suppose there are 5 checkboxes checked and if I am using `$_POST['check']`,shall I get all the checked id's?? – Techy May 12 '14 at 07:40
  • No. You'll get all the checked values. – Quentin May 12 '14 at 07:41
  • I have used `echo $var = ;` for creating checkboxes and used ` $class_ids =$_POST['class_id'];` for posting.It 'snt working – Techy May 12 '14 at 08:25
  • 1
    @Techy — Reread the first sentence of my answer. Then reread my first comment on it. **YOU NEED A VALUE** – Quentin May 12 '14 at 08:29
  • 1
    echo "< input type=\"checkbox\" name=\"class_id[]\" class=\"checkclasses\" value=\"$items[0]\" />"; – user3587554 May 12 '14 at 08:30
2

use the below change the $userid to whatever the variable is that has the id. This depends on how your creating the dynamic checkbox. When you pull the data from the database you need a while loop to display the data inside the form tag.

... more code should be here like connection and db selection
$query = "your query here";

while( $input = mysql_fetch_assoc( $quer) )
{
echo "<input type=\"checkbox\" name=\"id[]\" value=\"$input['your_id_column_here']\" />";
}

sample of the input

<input type="checkbox" name="id[]" value="$userid" />

then just loop over it

if( isset( $_POST['id'] ) ) // only if post data is there
{

$ids = $_POST['id'];

foreach($ids as $id)
{

// remove from db or whatever you want to here

}

}
user3587554
  • 353
  • 1
  • 7
  • I have used `echo $var = ;` for creating checkboxes and used ` $class_ids =$_POST['class_id'];` for posting.It 'snt working @user3587554 – Techy May 12 '14 at 08:27
  • since your not using a variable you need to encase the tag in "". You can;t assign a value to the name attribute the [] lets the browser know it's an array. You store the id of the info in the value attribute. – user3587554 May 12 '14 at 08:50