0

I have tried:

<input disabled='disabled' 
checked='checked' 
type='checkbox' 
name='checkTema[]' 
value=".$cat->cat_ID."> ".$cat->name."<br>"

But that won't pick up the value when I send the form

UPDATE

Thanks to a comment I've got a helping answer from another SO question, yet I am not sure how I should be applying the trick, tried the following but not sending the value

echo "<input type='hidden' name='checkTema[]' value='1' >";
echo "<input type='checkbox' checked='checked' disabled='disabled' name='checkTema[]' value=".$cat->cat_ID."> ".$cat->name."<br>";
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • @SougataBose `readonly` isn't picked up by the input ` ".$cat->name."
    ";`
    – rob.m Nov 23 '18 at 05:41
  • 3
    Possible duplicate of [Can HTML checkboxes be set to readonly?](https://stackoverflow.com/questions/155291/can-html-checkboxes-be-set-to-readonly) – Nick Nov 23 '18 at 05:42
  • `readonly` doesn't work on checkboxes. See my flagged duplicate for workarounds for this problem – Nick Nov 23 '18 at 05:43
  • I was not aware of that. Then you can use some hidden fields and jquery for this purpose. – Sougata Bose Nov 23 '18 at 05:44
  • @Nick thanks, saw that answer, but I don't want to use JavaScript, and I am using wrongly the hidden field trick. See updated question – rob.m Nov 23 '18 at 05:54

4 Answers4

0

If you need to POST disabled checkboxes, I suggest using a hidden input before:

<input type="hidden" name="checkbox_disabled" value="<?php echo $checkbox_value; ?>" /> <== Checked and disabled

Then in your PHP code, you can simply check:

if ($_POST['checkbox']) {
  //Checkbox wasn't disabled on submission
} else {
  //Fallback to using the disabled value:
  $disabledVal = $_POST['checkbox_disabled'];
}

Additionally, you can simply undisable all the elements of the form before submission:

$('form').submit(function() {
    $(this).find('input').attr('disabled', false);
});

See this question/answer for more information on how you can alternatively do this.

Blue
  • 22,608
  • 7
  • 62
  • 92
0

Try return false in onclick event

 <input 
     onclick="return false"
    checked='checked' 
    type='checkbox' 
    name='checkTema[]' 
    value='.$cat->cat_ID.'> ".$cat->name."<br>" 
bhaghyasandar
  • 516
  • 4
  • 16
  • well I'll be honest with you, this works as per https://stackoverflow.com/a/6905050/1018804 but I didn't want to use javascript. See my updated question as I believe I used wrongly the hidden field trick – rob.m Nov 23 '18 at 05:52
0

Try this code :

<input disabled='disabled' checked='checked' type='checkbox' name='checkTema[]' value="<?php echo $cat->cat_ID;?>"><?php echo $cat->name;?> 
Bhoomi Patel
  • 777
  • 10
  • 32
0

Thanks to a comment, I've got the solution in another SO question

Then my code changed to:

echo "<input type='hidden' name='checkTema[]' value=".$cat->cat_ID." >";
echo "<input type='checkbox' checked='checked' disabled='disabled'> ".$cat->name."<br>";
rob.m
  • 9,843
  • 19
  • 73
  • 162