tags:

views:

60

answers:

2

Hi All, The issue is I have a page with the following checboxes listed for a particular question.When I select one of the boxes and go to the next page and comeback then i find,none of the checkboxes appear to be checked.I have checked it on the back end and i was able to see that the checkboxes were indeed checked,but i was not able to view them as checked.I am not able to figure out as to why they dont appear to be checked.Any help regarding this would be appreciated.Thanks in Advance. The following is the code which i have in that page.

 <td>
  <input type="checkbox" name="test_na" value="N/A" <?=$test_na?> id="test_na">
  <label for="test_na">NA</label>
 </td>
 <td>
   <input type="checkbox" name="test_y" value="Y" <?=$test_y?> id="test_y"> 
    <label for="test_y">Yes</label>
 </td>

<td>
 <input type="checkbox" name="test_n" value="N" <?=$test_n?> id="test_n">
 <label for="test_n">No</label>
</td>
A: 

See page source. What in your variables? It should be checked="checked" or checked="yes" or checked="1"

Alexander.Plutov
No. You want `checked` for HTML or `checked='checked'` for XHTML. `yes` and `1` are invalid values.
David Dorward
Idea not in it. It should be a string with checked attribute in variables.
Alexander.Plutov
when i check all of them and then do an echo $test_na;it shows up as N/A instead of checked for the Option N/A.Am i missing something.
swathi
In your variables should be string with "checked". Can you show code with initializations of this vars?
Alexander.Plutov
@swathi — A submitted form will send the value of any checked input. Defaulting something to checked needs a checked attribute, which has nothing to do with whatever value you give it. If you are trying to get the same state then you have to check for the presence of the value in the submitted data and then output a checked attribute, you shouldn't just output the value as an attribute.
David Dorward
+4  A: 

Test the value of the checkoxes and echo checked if value matches.

<td>
  <input type="checkbox" name="test_na" value="N/A" <?php echo (isset($test_na) && $test_na == 'N/A' ? checked : ''); ?> id="test_na">
  <label for="test_na">NA</label>
 </td>
 <td>
   <input type="checkbox" name="test_y" value="Y" <?php echo (isset($test_y) && $test_y == 'Y' ? checked : ''); ?> id="test_y"> 
    <label for="test_y">Yes</label>
 </td>

<td>
 <input type="checkbox" name="test_n" value="N" <?php echo (isset($test_n) && $test_n == 'N' ? checked : ''); ?> id="test_n">
 <label for="test_n">No</label>
</td>
M42
@M42 :thank you very much.that worked
swathi
You're welcome.
M42