Jump to content

[SOLVED] determining which checkboxes in a form are checked


gamblor01

Recommended Posts

I have a page that will be generating unpredictable numbers of checkboxes (well unpredictable until the time the HTML is generated).  It's a data mining application and depending on the selection made by the user, they will presented with a table that may have 10 rows, 42, or 100.  My vision is to have a checkbox on each row, and allow the user to select any of the rows they want to throw out of the training set for my classifier.  Thus, I need to be able to submit the form to a php page and determine which checkboxes were selected because those are the data values that I need to throw out.

 

I know that if I had a form with a set number of fields this would be really easy.  But because this can have any number of fields, I'm not sure how to tell which boxes are checked and which ones are not.

 

Is this possible?  Thoughts?

Link to comment
Share on other sites

Well the data isn't random, but the data is being pulled by a SQL query that pulls from one of many possible tables.  Each table has a different number of rows in it, and it depends on which table the user selects.  If they select TableA then I need to print 30 rows on a page.  If they select table B then it's 7 rows, and so on.  So the generic PHP code generating this page is just taking the rows returned and going through a loop to generate the HTML table.

 

The idea is that the user would then be allowed to mark some of the rows as "questionable" and I need to know which rows those are.  If I can't do this with checkboxes I suppose I could do this with a text both where they simply enter the row numbers that they think are questionable.  Then I just pass that string when the form is submitted and I break it into separate tokens using comma/space/semicolon/colon/whatever as a delimiter.

 

I might just code this up to use a text box where you enter the rows in a list for now (just to get it working) but ultimately I would really like to get it working with checkboxes.  So far I don't really know how to do this without a gigantic if statement that has the same amount of conditions as the number of rows in the largest table.  That sounds like a bad design.  I'm just trying to figure out if there is something better.

Link to comment
Share on other sites

<input type="checkbox" value="box1" name="checks[]" />box 1
<input type="checkbox" value="box2" name="checks[]" />box 2
<input type="checkbox" value="box3" name="checks[]" />box 3

 

This will put all the checked boxes (and only the checked boxes) into an array. So if you were to check box 1 and box 3, and then used this code:

 

<?php
  print_r($_POST['checks']);
?>

 

The output would be:

 

Array ( [0] => box1 [1] => box3 ) 

 

 

Link to comment
Share on other sites

<input type="checkbox" value="box1" name="checks[]" />box 1
<input type="checkbox" value="box2" name="checks[]" />box 2
<input type="checkbox" value="box3" name="checks[]" />box 3

 

This will put all the checked boxes (and only the checked boxes) into an array. So if you were to check box 1 and box 3, and then used this code:

 

<?php
  print_r($_POST['checks']);
?>

 

The output would be:

 

Array ( [0] => box1 [1] => box3 ) 

 

Now THAT is the slick solution that I was looking for!

 

It works beautifully...thank you!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.