Yogi Yang 007
Yogi Yang 007

Reputation: 5251

Enumerate all Check Box in PHP

I have web page in PHP which displays all records in a table. I want to add check boxes against all rows and user can check a check box to select a row and then submit the page. When the page is submitted I want to enumerate all check boxes and check whether they are checked or not, How can I do this?

Upvotes: 1

Views: 673

Answers (4)

risyasin
risyasin

Reputation: 1321

if i were you... i wouldn't fight with altering html table structure. you can handle that with Javascript frameworks like JQuery which is very effective solution for you. you deal only a few lines of JS code and you don't need exaggerate the html output (i guess it's probably long enough). about jquery there is a good source named visual jquery if you have never used that.

here is the way how to do that. you dont need to edit inside the loop. you just only put an id to your table tag. then add new column to your table with checkboxes inside. then you can get the values of checkboxes & serialize them in to a hidden input. or you can handle selected rows with ajax easy. i think JS framework will work better.

normally i've added many links to post but it says it's not allowed for new users.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655269

You don’t have to check all checkboxes if they have been checked. Because only successful controls are send to the server. And a checkbox is only successful when it’s checked:

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful.

So you just have to look what checkboxes you get in the request at all. And if you want to use <select multiple>, take a look at How do I get all the results from a select multiple HTML tag? in the PHP FAQ.

Upvotes: 1

Andreas
Andreas

Reputation: 5335

Creating the form

You can generate the HTML as follows:

<form [action, method etc]>
  <table>
  <?php 
    foreach($dataSet as $dataRow) : 
  ?>
      <tr>
         <td>
            <input type="checkbox" name="dataRow[]" value="<?=$dataRow['id']?>"/>
         </td>
         [Additional details about datarow here]
      <tr>
  <?php 
    endforeach; 
  ?>
  </table>
</form>

AFTER POST

look into $_POST['dataRow'] : this will be an array with values the IDS of your $dataRow, so using array_values on $_POST['dataRow'] will give you all the ids of the selected rows:

<?php
   $checkedRows = array_values($_POST['dataRow']);
   foreach($checkedRows as $row) {
      // Do whatever you want to do with the selected row
   }

Upvotes: 2

deceze
deceze

Reputation: 522125

You'll create your checkboxes like this:

<input name="rows[]" value="uniqueIdForThisRow" type="checkbox" />
<input name="rows[]" value="anotherId" type="checkbox" />

Then you can loop through them like this:

<?php
    // $_POST['rows'] contains the values of all checked checkboxes, like:
    // array('uniqueIdForThisRow', 'anotherId', ...)

    foreach ($_POST['rows'] as $row) {
        if ($row == 'uniqueIdForThisRow') {
            // do something
        }
    }
?>

PHP docs on dealing with forms, see especially Example #3.

Upvotes: 5

Related Questions