Actually, your code is working, as long as $val == 'on'
.
Did you try debugging by doing echo $val
?
If it’s still not working, try hard refreshing (Ctrl + F5) your webpage, checkboxes tend to keep their state on normal refresh (F5).
Also, please put your code directly on this forum, so the code is always accessible for future viewers 😉