-1

Please have a look on this code. How can I implement this?

enter image description here

Lately i was done like this -

<script type="text/javascript">
function CheckPowerAll() {
    if (document.getElementById("PO_ALL").checked == true) {

        document.getElementById("PO_PowerSteering").checked = true;
        document.getElementById("PO_PowerMirrors").checked = true;
    } else {

        document.getElementById("PO_PowerSteering").checked = false;
        document.getElementById("PO_PowerMirrors").checked = false;
    }
}
</script>

<tr>
  <td><input name="PO_ALL" type="checkbox" id="PO_ALL" value="checkbox" onclick="CheckPowerAll()" />
Select all <span class="bold">Power Options</span> </td>
   </tr>
 <tr>
<td><table width="85%" border="0" cellspacing="0" cellpadding="0">
 <tr>
<td class="box2">

   <input name="PO_PowerSteering" type="checkbox" id="PO_PowerSteering" value="Power Steering" />
Power Steering<br />
   <input name="PO_PowerMirrors" type="checkbox" id="PO_PowerMirrors" value="Power Mirrors" />
Power Mirrors <br /></td>
  </tr>
</table></td>
  </tr>
<tr>

But now I need to populate the value from DB.

<input name="PO_ALL" type="checkbox" id="PO_ALL" value="checkbox" onclick="CheckPowerAll()" />
Select all <span class="bold">Power Options</span> </td>
   </tr>
 <tr>
<td><table width="85%" border="0" cellspacing="0" cellpadding="0">
 <tr>
<td class="box2">

<?php 

$query = mysql_query("SELECT * FROM vehicle_poweroptions"); 
    while ( $results[] = mysql_fetch_object ($query));
      array_pop ( $results );
        foreach ( $results as $option ) : ?>

<input name="PO_PowerWindows" type="checkbox" id="PO_PowerWindows" value="<?php echo  $option->id; ?>" />

<?php echo  $option->type; ?><br />

<?php endforeach; ?> 

How can I implement this?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Thomas Tkr
  • 49
  • 1
  • 9
  • 2
    Not related to your answer but this `name="PO_PowerWindows"` should be `name="PO_PowerWindows[]"` – Mr. Alien May 30 '13 at 05:21
  • 1
    check this link http://stackoverflow.com/questions/386281/how-to-implement-select-all-check-box-in-html – cartina May 30 '13 at 05:30

3 Answers3

3

Working jsFiddle Demo

You can't have multiple elements with same ID. You must change your foreach loop. I've deleted the id attribute totally. I'll work with name attribute:

<?php foreach ( $results as $option ) : ?>
    <input
        name="PO_PowerWindows"
        type="checkbox"
        value="<?php echo  $option->id; ?>"
    />
<?php endforeach; ?> 

As you see in the code, I change the id to the class.

And here is your select all checkbox (I didn't make any changes on it):

<input name="PO_ALL" type="checkbox" id="PO_ALL" value="checkbox" onclick="CheckPowerAll()" />

And your function would be something like this:

function CheckPowerAll() {
    var elements = document.getElementsByName("PO_PowerWindows");
    var l = elements.length;

    if (document.getElementById("PO_ALL").checked) {
        for (var i = 0; i < l; i++) {
            elements[i].checked = true;
        }
    } else {
        for (var i = 0; i < l; i++) {
            elements[i].checked = false;
        }
    }
}
  • @ThomasTkr You're welcome, I also change a little my function to make it compatible with old version of IE too `;)`. –  May 30 '13 at 05:54
1
<input name="PO_PowerWindows[]" type="checkbox" id="PO_PowerWindows" value="<?php echo  $option->id; ?>" checked="checked"/>
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
1

just add

array_pop ( $results ); ?>
  <input name="PO_ALL" type="checkbox" id="PO_ALL" value="checkbox" onclick="CheckPowerAll()" />
<?php    foreach ( $results as $option ) : ?>
Engineer
  • 5,911
  • 4
  • 31
  • 58