0

I am processing an html form using php. My question is specifically about input type checkbox:

<input type="checkbox" name="checkme" value="checked" <***php echo $data['checkme']; ***> >

This works for me because, on load, $data['checkme'] = ""; and on error, $data['checkme'] = "checked". I have searched quite a bit regarding this, and there are plenty of suggestions for setting the value of the checkbox input. But not in this way (that I found). This works, but I want to make sure I am not creating a problem that I don't foresee.

My question: Is this good practice?

3
  • Is checked HTML5? I thought previous format was checked="checked". Anyway, back to the real question, I usually have it as if(!empty().. { echo 'checked="checked"';} Commented Feb 18, 2022 at 19:50
  • The way you found seems like first answer here, stackoverflow.com/questions/16239663/…. With the exception that if $data or checkme don't exist you'll throw a notice. Commented Feb 18, 2022 at 19:52
  • The form initializes with $data['name'], $data['address'],...etc. The array values are empty upon initialization, and populated with user input if the user needs to make correction. If $data doesn't exist, every field of the form will throw a notice. I appreciate the link to question # 16239663. I had not found that in my previous search. Thanks Commented Feb 20, 2022 at 23:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.