tags:

views:

53

answers:

2

how can I make users check on textclick? My script is:

 <?php 
      foreach($getcountry_name as $key => $value)
 { ?>
    <tr>
        <td width="25" valign="top">
        <input name="country_name[]"  
             type="checkbox" checked="checked" 
             value="<?php echo $value['country_id']; ?>" />
        </td>
        <td>
        <label class="type01"><?php echo $value['country_name']; ?></label>
        </td>
    </tr> 
   <?php
     }
   ?>        

thanks in advance

A: 

Give the inputan ID and let your label use this as the value for a for attribute.


<tr>
    <td width="25" valign="top">
        <input id="<?php $id=uniqid('id_'); echo $id; ?>" name="country_name[]" type="checkbox" checked="checked" value="<?php echo $value['country_id']; ?>" />
    </td>
    <td>
        <label for="<?php echo $id; ?>" class="type01"><?php echo $value['country_name']; ?></label>
    </td>
</tr>
jensgram
Hmm .. yeah, you can probably just use the `country_id` if it qualifies for a HTML ID. ... or the `$key` from the newly edited `for` loop :)
jensgram
+5  A: 

The <label> needs a for attribute that contains the ID of the <input> field it's linked to.

e.g.

<input id="myID" name="country_name[]"  type="checkbox" checked="checked" value="<?php echo $value['country_id']; ?>" />
<label for="myID" class="type01"><?php echo $value['country_name']; ?></label>

Edit:

Applying it to your example,

<?php 
      foreach($getcountry_name as $key => $value)
 { ?>
    <tr>
        <td width="25" valign="top">
        <input id="country_name_<?php echo $value['country_id']; ?>" name="country_name[]"  
             type="checkbox" checked="checked" 
             value="<?php echo $value['country_id']; ?>" />
        </td>
        <td>
        <label for="country_name_<?php echo $value['country_id']; ?>" class="type01"><?php echo $value['country_name']; ?></label>
        </td>
    </tr> 
   <?php
     }
   ?>  
Stephen Melrose
Note that one can do it implicitly as well by wrapping the checkbox in the label: <label><input type="checkbox" name="foo" value="bar" />Clickable text goes here</label>. Do note that it's deprecated as of some XHTML version (can't remember which)
antennen
@antennen ... and not possible across table cells.
jensgram
@antennen: Which only matters if you use XHTML, which most people don't (although it appears the OP is, from the end of their input tag). It's fine in HTML, right up through HTML5: http://dev.w3.org/html5/spec/Overview.html#the-label-element Not for every situation (as jensgram points out), but...
T.J. Crowder
thank u very much
praveen